Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is static method faster than non-static?

EDIT::oh i forgot

class Test1{
    public static function test(){
        for($i=0; $i<=1000; $i++)
            $j += $i;       
    }   
}

class Test2{
    public function test() {
        for ($i=0; $i<=1000; $i++){
            $j += $i;
        }
    }

}

for this algorithm

$time_start = microtime();
$test1 = new Test2();
for($i=0; $i<=100;$i++)
    $test1->test();
$time_end = microtime();

$time1 = $time_end - $time_start;

$time_start = microtime();
for($i=0; $i<=100;$i++)
    Test1::test();
$time_end = microtime();    

$time2 = $time_end - $time_start;
$time = $time1 - $time2;
echo "Difference: $time";

i have results

Difference: 0.007561 

and these days, i am trying to make my methods static as possible. But is it really true, .. atleast for php

like image 221
Santosh Linkha Avatar asked Jan 24 '11 08:01

Santosh Linkha


People also ask

Is static method faster than instance method?

The static method can call without object while instance method can not be called without object. The execution time of instance method is faster than static method but in the PHP version 5.3 there was a bug which says that static methods are faster in which they have introduced late binding.

Are static methods slow?

Conclusion. The results show that calls to static methods are indeed faster than the equivalent calls to instance methods.

Are static methods slower Java?

That said, static methods are almost certainly not slower than any instance method, in most cases marginally faster: 1.) Static methods are not polymorphic, so the JVM has less decisions to make to find the actual code to execute.

Which is better static or non-static Java?

In the static method, the method use compile-time or early binding. For this reason, we can access the static method without creating an instance. In a non-static method, the method use runtime or dynamic binding. So that we cannot access a non-static method without creating an instance.


2 Answers

You should always use static when you don't need an object around you method, and use dynamic when you need an object. In the example you provides, you don't need an object, because the method doesn't interact with any properties or fields in your class.

This one should be static, because it doesn't need an object:

class Person {
    public static function GetPersonByID($id) {
        //run SQL query here
        $res = new Person();
        $res->name = $sql["name"];
        //fill in the object
        return $res;
    }
}

This one should be dynamic, because it uses the object it is in:

class Person {
    public $Name;
    public $Age;
    public function HaveBirthday() {
        $Age++;
    }
}

The speed diffirence is minimal, but you have to create an object to run dynamic methods, and that object is saved in memory, so dynamic methods use more memory and a little more time.

like image 89
Jan Sverre Avatar answered Oct 07 '22 17:10

Jan Sverre


Short Answer since I don't want to go on a rant to much:

It doesn't matter if it's faster. If you need something where performance is THAT important that you think about shaving of 0.02 nanoseconds per function call than you're not going to do it in PHP anyways.

Static methods make for untestable, unmaintainable, "global everything" code that is going to hurt you much more than anything else.

If you don't want to use proper OOP (and thats totally fine if you know what and why you are doing it) then please do so. Just don't do it because you want to save cpu time.

http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/

http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html

http://en.wikipedia.org/wiki/Class-based_programming

If you only click one link: http://www.scribd.com/doc/20893084/Advanced-OOP-and-Design-Patterns#scribd

Premature optimization is the root of all evil. Build code that is easy to maintain and if it's getting slow take a profile and it will most likely tell you that the filesystem oder the database is problem, one you got all that sorted out there will be some very specific pieces of php to optimize. If you got clean, changeable code you can speed those up.

like image 31
edorian Avatar answered Oct 07 '22 16:10

edorian