• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

Speed test!?

Joined
Dec 15, 2009
Messages
1,387
Reaction score
236
Method 1:
Read my comments.
PHP:
	if(file_exists($cache) && (time() - filemtime($cache) < $cacheInterval)) {
		$im = imagecreatefrompng($cache); // instead of calling it from the class file, I decided to left it to run individually to reduce loading speed.
		imagesavealpha($im, true);
		imagePNG($im);
		imageDestroy($im);		
	} else {
		require_once('coordinates.php');
		$Image = new Coordinates;

		//calling functions from class file
	}

Method 2:
PHP:
require_once('coordinates.php'); // stored outside of 'if else' statements
$Image = new Coordinates;
	if(file_exists($cache) && (time() - filemtime($cache) < $cacheInterval)) {
		//calling functions from class file
	} else {
		//calling functions from class file	
	}

Assuming the class file is approximately 5KB(lots of functions) and the main page is only around 1KB.
Which one should I go for?

*Note: if i go for Method 2, will it affect the loading time even I am not calling most of the functions from the class file?
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Loading a class file in PHP will load the entire class with all methods. If you're frequently not using the majority of methods/properties in a given class, then you should break it up into sub-classes to reduce the file-size. Look into autoloaders. Keep in mind the purpose of OOP is to make your code easier to manage and decrease development time, not to increase execution speed. Your OOP code can still be rather efficient, but the code will do what you tell it to do- load the entire class and all methods, even though you're only using a small portion of the class. In general, you want to keep your classes small and use an autoloader to make loading multiple files a single time (and only when needed), as simple as possible. There's a give-and-take there, too, as file I/O is a blocking task. Therefore, too many small classes that you use frequently is rather slow and possibly redundant.

In conclusion, make more class files with less dependencies (loose-coupling), but try not to repeat functionality (DRY). If you find a noticeable difference in execution time from procedural testing and the tests you perform from your classes, there's a reason for that. There shouldn't be a big difference. The problem isn't the fact you're using a class, it's the way you're using the class, or rather, what the class is doing that is the problem.
 
Back
Top