What will work faster? How strongly will it affect productivity?
$test = new Test();
$a1 = array();
$a2 = array();
$a3 = array();
$a1[1] = $test;
$a2['qwe'] = $test;
$a3[] = $test;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As no one answered, I’ve made some test
The code:
The results:
Third one is the slowest one, but mostly because it actually allocates million objects and first two overwrite object on each pass. When I modified first to
$a1[$k] = $test;, the results were similar (although don’t run it on a low memory limit, say below 128MB).The conclusion: as we said before, it doesn’t really matter. Focus on writing code which is readable and utilizes design patterns, not on some minor optimizations.