Hi I recently saw this article on how to store data into objects:
$profile = new stdClass;
$profile->first_name = 'Woppi';
$profile->last_name = 'Jillenjack';
$profile->email = 'woppi.jillenjack@gmail.com';
I’m used to making it an array then typecasting it to object (before I read the article):
$profile = array('first_name'=>'Woppi', 'last_name'=>'Jillenjack', 'email'=>'woppi.jillenjack@gmail.com');
$profile = (object)$profile;
I’m thinking the first method is faster. How do I know which is faster… can you suggest a way I can find this out?
Thank you very much.
Well if you are running PHP on Linux you can measure the time to run each method with microtime()
* Windows won’t return correct time but an approximation. That won’t really help you if you are trying to measure something in microseconds.
Also I believe that the second method should be faster because it will use an integrated function written in C, instead of interpreting several lines of PHP code and then executing them.
So I vote for the second one. You can still have some fun with measuring how much time it will take for each method.