Skip to content

Instantly share code, notes, and snippets.

@lega911
Created October 11, 2015 12:22
Show Gist options
  • Save lega911/c4b627c3f17625f05d44 to your computer and use it in GitHub Desktop.
Save lega911/c4b627c3f17625f05d44 to your computer and use it in GitHub Desktop.
// 0.403273203 sec
package main
import "fmt"
import "time"
func main() {
start := time.Now().UnixNano()
for i := 0; i < 1000000000; i++ {
}
finish := time.Now().UnixNano()
dur := float64(finish-start)
fmt.Println(dur / 1000000000)
}
# 41.40947604179382 sec
import time
start = time.time()
for i in range(1000000000):
pass
finish = time.time()
print(finish-start)
@nin-jin
Copy link

nin-jin commented Oct 11, 2015

А вообще, эквивалентный код на питоне будет таким:

import time

start = time.time()
i = 0
while i < 1000000000:
    ++i

finish = time.time()

print(finish-start)

@nin-jin
Copy link

nin-jin commented Oct 11, 2015

У вас питон-то хоть третьей версии? :-)

@nin-jin
Copy link

nin-jin commented Oct 13, 2015

Я тут запилил эквивалент на D - в дебажном режиме в 4 раза медленнее Go, в релизе - в 2 раза быстрее.

package main

import "fmt"
import "time"

func main() 
{
    start := time.Now().UnixNano()

    accum := 0
    for i := 0; i < 1000000000; i++ {
        accum += i
    }

    finish := time.Now().UnixNano()

    dur := float64(finish-start)
    fmt.Println(dur / 1000000000)
}
import std.stdio;
import std.datetime;

void main() 
{
    StopWatch timer;
    timer.start();

    int accum = 0;
    for( int i = 0 ; i < 1000000000 ; i++ ) {
        accum += i;
    }

    writeln( timer.peek().msecs / 1000.0 );
}
> go run test.go
0.5103397

> dub
2.136

> dub --build=release
0.297

@vovanbo
Copy link

vovanbo commented Oct 15, 2015

Добавлю свои 5 копеек по питончику. Я, правда, не понял то ли у вас тут выше код суммирует числа от 1 до миллиарда, то ли суммирует миллиард единичек. Но я и такой и такой тест сделал.

import sys
import timeit
import itertools

executions = 10

def sum_of_one():
    return sum(itertools.repeat(1, 1000000000))

def accum():
    return sum(range(1000000000))

sum_of_one_time = timeit.timeit(sum_of_one, number=executions)
accum_time = timeit.timeit(accum, number=executions)

print(sys.version)
print('Executed %s times' % executions)
print('Sum of 1', sum_of_one_time, sum_of_one_time/executions)
print('Accumulate from 1 to 1000000000', accum_time, accum_time/executions)

Результаты:

Python 3.5.0:

3.5.0 (default, Oct 15 2015, 12:52:25)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)]
Executed 10 times
Sum of 1 96.66175519199987 9.666175519199987
Accumulate from 1 to 1000000000 249.16431563599963 24.916431563599964

PyPy

2.7.10 (f3ad1e1e1d62, Aug 28 2015, 09:36:42)
[PyPy 2.6.1 with GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]
Executed 10 times
('Sum of 1', 12.96062684059143, 1.2960626840591432)
('Accumulate from 1 to 1000000000', 12.47924280166626, 1.247924280166626)

PyPy3

3.2.5 (b2091e973da6, Oct 19 2014, 18:30:58)
[PyPy 2.4.0 with GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)]
Executed 10 times
Sum of 1 12.533483028411865 1.2533483028411865
Accumulate from 1 to 1000000000 12.479850053787231 1.2479850053787231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment