On 16/04/2017 16:00, justin walters wrote:
On Sun, Apr 16, 2017 at 3:55 AM, bartc <b...@freeuk.com> wrote:

Example C of the same silly program in Python:

def add(a,b):
    return a+b

def testfn():
    sum=a=b=0
    for i in range(100000000):
        sum += add(a,b)
        a += 1
        b += 2

    print (a,b,sum)

testfn()


Timings:

A (Pure HLL**)          13   seconds      (dynamic/interpreted)
A (With ASM module)      3

B (my compiler)          0.5              (static/compiled)

C (Python 2/xrange)     30
C (Python 3)            38
C (Pypy)                 5



Just for fun I wanted to write this up in Nim to compare execution time.
Nim has Python-esqe syntax but is statically

(Nice-looking language, I hadn't heard of it.)

typed and compiled. I think this is relevant to the discussion.

Code looks like this:

```
import times

proc add(a, b: int): int =
    result = a + b

proc test() =
    var
        sum = 0
        a = 0
        b = 0
    for i in 0..<100000000:

Loop iterations should be 100 million, if this is one less. Missing one out won't affect the timing, but will give a different result if comparing implementations to see if they do the same thing.

With 100M, the results are (of a,b and sum):

100000000 200000000 14999999850000000

        sum += add(a, b)
        a += 1
        b += 1

b += 2, both to the ensure the same output, and in case there's a sneaky optimisation it can do for b+=1...

    echo "a: " & $a & " b: " & $b & "\n"
    echo "Sum: " & $sum

when isMainModule:
    var t0 = cpuTime()
    test()
    var t1 = cpuTime()
    echo "***Execution Time: " & $(t1 - t0) & "***\n"
```


No optimization: ***Execution Time: 2.876923***
Optimized for speed: ***Execution Time: 2.844163***
Optimized for size: ***Execution Time: 2.844901***

Hmm, the optimiser is similar to mine then!

Release option: ***Execution Time: 2.844021***

So, generally around 2.8 seconds.

Not too bad for a GC'd language. There are probably some more optimizations
I could make to improve execution time.

What were the results with Python on your machine?

--
bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to