Static typing [was Re: Python and the need for speed]

justin walters walters.justin01 at gmail.com
Sun Apr 16 12:20:18 EDT 2017


On Sun, Apr 16, 2017 at 8:46 AM, bartc <bc at freeuk.com> wrote:

> What were the results with Python on your machine?



Well, at first I tried to implement it with a generator. I gave up on
waiting for the program to complete
after about 6 minutes.

After using your code almost exactly I get:
>>> Sum:  14999999850000000
>>> ***Execution Time:  17.33912420272827 ***

That's with Python 3.5 on an Intel I5-6600k 3.9Ghz cpu and 16GB DDR4 memory.

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

Yeah, it's pretty neat. Some parts of Nim aren't really production ready
yet, but I think it has a bright future.

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.
>

It's not one less. It will execute exactly 100 million times. Meaning it
will execute when i is
99,999,999 but not when i is 100,000,000. This gives exactly 100,000,000
iterations including
the iteration for i = 0. It could alternatively be written as:

============================================
for i in 1..100000000:
============================================

Didn't realize I had messed up the increment on var b with my Nim
implementation!

Fixed:

===========================================
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:
        sum += add(a, b)
        a += 1
        b += 2
    echo "a: " & $a & " b: " & $b & "\n"
    echo "Sum: " & $sum

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

The above Nim code is a tad bit slower with the larger var b:
>>> a: 100000000 b: 200000000
>>> Sum: 14999999850000000
>>> ***Execution Time: 2.888533***

With Speed Optimization:
>>> a: 100000000 b: 200000000
>>> Sum: 14999999850000000
>>> ***Execution Time: 2.843629***

With size optimization:
>>> a: 100000000 b: 200000000
>>> Sum: 14999999850000000
>>> ***Execution Time: 2.844876***

Compiled with release flag:
>>> a: 100000000 b: 200000000
>>> Sum: 14999999850000000
>>> ***Execution Time: 2.842312***

    > Hmm, the optimiser is similar to mine then!

I'm pretty new when it comes to compilers. Been trying to learn more and
build my own but
progress is fairly slow. I'm not sure what `nimc` is doing under the hood,
but I do know that it
transpiles to C before running either `gcc` or `clang`.


More information about the Python-list mailing list