概要
Python3、JavaScriptとBrythonの実行時間を比較してみる。
数値演算
Python/Brythonコード
1 2 3 4 5 6 7 8 9 10 11 |
import time start = time.time() a = 1 b = 2 for i in range(1000000): c = a + b elapsed_time = time.time() - start print("elapsed time: %fsec" % elapsed_time) |
JavaScriptコード
1 2 3 4 5 6 7 8 9 10 11 |
var startTime = ( new Date() ).valueOf(); var a = 1; var b = 2; for ( i = 0; i < 1000000; i++ ) { var c = a + b; } elapsedTime = ( new Date() ).valueOf() - startTime; console.log( "elapsed time: " + parseFloat(elapsedTime).toFixed(6) + "sec" ); |
結果
経過時間 | |
Python3 | 0.152009sec |
Brython | 3.068000sec |
JavaScript | 3.000000sec |
配列の確保
Python/Brython
1 2 3 4 5 6 7 8 9 |
import time start = time.time() for i in range(1000): a = [0 for i in range(100)] elapse = time.time() - start print("elapsed time: %fsec" % elapse) |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 |
var startTime = ( new Date() ).valueOf(); var array = []; for ( i = 0; i < 1000; i++ ) { for ( j = 0; j < 100; j++ ) { array[j] = 0; } } elapsedTime = ( new Date() ).valueOf() - startTime; console.log( "elapsed time: " + parseFloat(elapsedTime).toFixed(6) + "sec" ); |
結果
経過時間 | |
Python3 | 0.006001sec |
Brython | 8.212000sec |
JavaScript | 2.000000sec |