PHP Speed test

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
kitman
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Nov 23, 2017 8:51 pm

Thu Nov 23, 2017 9:03 pm

I was doing research about speed, however I hit into something I don't understand, so want someone the explain to me , thanks

Case 1:

Code: Select all

 
     private function abcdefg(){
        return 1;
    }
public function testfunction()
 {
$startTime = microtime(true);
$data = $this->abcdefg();
return "Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";
}
the result comes out
Time: 0.2101 Seconds
Time: 0.2071 Seconds
Time: 0.1918 Seconds
Time: 0.2040 Seconds
Time: 0.1918 Seconds

Case 2:
However

Code: Select all

    private function abcdefg(){
        return 1;
    }
public function testfunction()
 {
        $startTime = microtime(true);
        $abc = $this->abcdefg();
        $data = $abc;
        return  response()->json("Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n");
}
The result comes out:
Time: 0.1757 Seconds
Time: 0.1846 Seconds
Time: 0.1703 Seconds
Time: 0.1877 Seconds
Time: 0.1897 Seconds

I don't understand why the 2 one is faster than the 1 one, can someone explain to me thanks.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 648
Joined: Mon Feb 22, 2016 5:52 pm

Fri Nov 24, 2017 11:56 am

It does seem counter-intuitive, however, this is not a simple subject as there are many things going on that need to be taken into account.

1) PHP is an interpreted language, this means that it depends on how PHP has interpreted your intentions when writing the code, just because you have more PHP instructions, doesn't mean that the computer will have to execute more code at CPU level

2) Different versions of PHP have worked on optimising certain parts of the PHP interpreter, if you optimised for one version of PHP, it may change when upgrading, a change somewhere else in the script could possibly affect the overall timings

3) Making timings of script execution is always a contentious issue, it will depend on what's running in the background - have you tried executing the scripts over a long period of time at different times of the day? Or did you just execute the scripts one after the other?

The long and the short of it is: if a script takes over a second or two to execute, then it would be worth looking at speed; this I hope would have been some advice that you have already come across during your " research about speed". Most speed issues are using too many unnecessary / convoluted loops and database calls.

I hope that this goes some way to you understanding the why

Post Reply