Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

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