Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is PHP7 so much faster than Python3 in executing this simple loop?

As an extremely simple benchmark, I executed the below simple code on PHP 7.0.19-1 and Python 3.5.3 (command line) on the same Raspberry Pi 3 model B.

Python's execution time was horrible in comparison to PHP's (74 seconds vs 1.4 seconds). Can anyone help me understand why the execution takes so much longer on Python? Is there something I'm doing wrong, or some optimizations/settings that would improve its performance to meet or exceed that of PHP? Or is Python just that much slower (surely not!)?

Yes I saw this benchmark, which reports PHP 7 blazes past the other languages, but you'd think both would be fairly equally optimized when doing such a simple operation.

Python executes the loop about twice as fast if a string assignment is substituted for the addition. But that's still 34 seconds vs about 1.1 sec.

PHP7 code:

<?php

function test($x)
{
    $t1 = microtime(true);
    $a = 0;
    for($i = 0; $i < $x; $i++)
    {
        $a++;
    }
    $t2 = microtime(true);

    echo "Time for $x was " . ($t2 - $t1) . "\n";

    return $a;
}


echo test(100000);
echo test(1000000);
echo test(10000000);

Results: Time for 100000 was 0.036377191543579 100000Time for 1000000 was 0.18501400947571 1000000Time for 10000000 was 1.3939099311829

Python3 code:

import time
def test(x):
    t1 = time.clock()
    a = 0
    for i in range(x):
        a += 1
    t2 = time.clock()
    print("Time for {} was {}".format(x, t2 - t1))
    return x

print(test(1000000))
print(test(10000000))
print(test(100000000))

Results: Time for 1000000 was 0.761641 1000000 Time for 10000000 was 7.427618000000001 10000000 Time for 100000000 was 74.320387 100000000

UPDATE: yes after @Amber pointed it out, I realize I totally PEBKAKed and the loop counters are an order of magnitude apart. Even so, the answers were really interesting so it was worth asking the question.

like image 305
Ryan Griggs Avatar asked Dec 30 '17 05:12

Ryan Griggs


People also ask

Is PHP faster than Python?

Each time a file is created or modified; Python converts the code into bytecode. This code compilation method makes Python quicker than PHP. PHP programmers can simply improve the speed of PHP applications by installing a variety of caching systems. By default, Python is a faster language.

Is PHP faster than Python 7?

It's exceptionally faster than many programming languages, including Python. Zend Engine 3.0 was also released with PHP 7, making the programming language 2x faster than its previous version. Comparatively, Python's code compilation process is designed to be quicker, even without installing caching systems.

Is PHP fast enough?

On average, it takes less time to deliver a project using PHP than when some other programming language is chosen and used for the task at hand. Less time means your project is going to be released faster.

Can PHP and Python work together?

Yes it will work, and how risky it is depends on how good your implementation is. This is perfectly acceptable if done correctly. I have successfully integrated PHP and C, when PHP was simply too slow to do certain niche tasks in real time (IIRC, PHP is 7 times slower than its C counterpart).


2 Answers

They're both within an order of magnitude of each other, when you run them with identical cycle counts rather than having the Python counts being larger by an order of magnitude:

PHP: https://ideone.com/3ebkai 2.7089s

<?php

function test($x)
{
    $t1 = microtime(true);
    $a = 0;
    for($i = 0; $i < $x; $i++)
    {
        $a++;
    }
    $t2 = microtime(true);

    echo "Time for $x was " . ($t2 - $t1) . "\n";

    return $a;
}


echo test(100000000);

Python: https://ideone.com/pRFVfk 4.5708s

import time
def test(x):
    t1 = time.clock()
    a = 0
    for i in range(x):
        a += 1
    t2 = time.clock()
    print("Time for {} was {}".format(x, t2 - t1))
    return x

print(test(100000000))
like image 122
Amber Avatar answered Oct 18 '22 01:10

Amber


You guys are not being fair. The two pieces of code are NOT doing the same thing.

While PHP only increments two variables ($a and $i), Python is generating a range before it loops.

So, to have a fair comparison your Python code should be:

import time
def test2(x):
    r = range(x) #please generate this first
    a = 0

    #now you count only the loop time
    t1 = time.clock()
    for i in r:
        a += 1
    t2 = time.clock()

    print("Time for {} was {}".format(x, t2 - t1))
    return a

Aaaaaaand, it's MUCH faster:

>>> print(test(100000000))
Time for 100000000 was 6.214772

VS

>>> print(test2(100000000))
Time for 100000000 was 3.079545
like image 32
Rafael Beckel Avatar answered Oct 17 '22 23:10

Rafael Beckel