LoginSignup
5

More than 5 years have passed since last update.

ベンチマークの簡易スクリプト

Last updated at Posted at 2015-03-10

Rubyが遅いという噂だったので、ベンチマーク用のスクリプトを作って実験してみました。

擬似乱数を1000行出力する(つもりの)スクリプトです。
  

Ruby -v 1.9

require 'benchmark'

result = Benchmark.realtime do
  (1..1000).each do |x|
  randam = Random.new
    randam.rand(1000)
    x += 1
  end
end
puts "END: #{result}s"

結果  

END: 0.016832s

2015/03/10 追記
@scivolaさんから指摘いただいたところを修正したスクリプト
ruby -v ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]

require 'benchmark'

randam = Random.new
result = Benchmark.realtime do
  (1..1000).each do |x|
    randam.rand(1000)
  end
end
puts "END: #{result}s"
END: 0.00010036501043941826s

  
Python 2.7  

import random
import timeit

def hoge():
    random.randrange(1000)

t = timeit.timeit(stmt=hoge, number=1000)

print "END: %fs"%(t)

結果

END: 0.000763s

  
C

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
  int i, a;
  clock_t start, end;


  start = clock();
  srand( (unsigned int)time(0));

  for(i = 0 ; i < 1000 ; i++){
   a = rand() % 1000;
  }
  end = clock();
  printf("END: %fs \n", (double)(end - start) / CLOCKS_PER_SEC);
}

結果

END: 0.000017s

  
比率というか順番というものは、ググって出てくるデータとだいたい一緒な気がしますね。。。
Rubyはコードが綺麗なので、簡単なWebアプリケーションの作成やプログラミングの勉強には最も向いていると思っている!  

はい。  

再実験

  
2015/03/15 追記
randam.rand(1000)をブロックに入れておく必要はなかったようで  
処理もかなり速くなりPythonを抜く結果になりました。(^ ^)

インクリメントをつけていたのは謎です(´ω`)‥トホー  

しかし‥ 

乱数を発生させるだけのベンチマークだと,言語処理系はあまり関係なくて,
内蔵している乱数発生器の性能を測ることになるんじゃないかな

たしかにですね。そんな気がします。
という訳で出力もするテストを実施してみました。
Pythonのprint、Cのprintfに合わせて改行させるのでRubyはPutsを使っています。
  
Ruby2.2.0p0 

require 'benchmark'

randam = Random.new
result = Benchmark.realtime do
  (1..1000).each do |x|
    puts randam.rand(1000)
  end
end
puts "END: #{result.to_f}s"
$ ruby bench.rb > rubybench.txt
$ tail rubybench.txt
714
905
522
713
861
615
240
626
126
END: 0.0006876440020278096s
$ wc -l rubybench.txt
    1001 rubybench.txt

python 2.7.6

import random
import timeit

def hoge():
    print random.randrange(1000)

t = timeit.timeit(stmt=hoge, number=1000)

print "END: %fs"%(t)
$ python bench.py > pythonbench.txt
$ tail pythonbench.txt
754
786
919
950
16
294
18
266
62
END: 0.001284s
$ wc -l pythonbench.txt
    1001 pythonbench.txt

c gccコンパイラ

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
  int i, a;
  clock_t start, end;


  start = clock();
  srand( (unsigned int)time(0));

  for(i = 0 ; i < 1000 ; i++){
   a = rand() % 1000;
   printf("%d\n", a);
  }
  end = clock();
  printf("END: %fs \n", (double)(end - start) / CLOCKS_PER_SEC);
}
$ gcc -o hoge.o bench.c
$ ./hoge.o > benchc.txt
$ tail benchc.txt
12
589
344
998
668
221
426
11
508
END: 0.000167s
$ wc -l benchc.txt
    1001 benchc.txt

行数をカウントするのでリダイレクトでテキストに出力するという工程を挟んでみました。
標準出力させない結果ですね。
結果は
C、Ruby、Python
の順に速いという結果になりました。

裏話

tailやテキストに出力ではなくて、端末に標準出力させた結果を書きます。
Ruby
END: 0.00417862601170782s
END: 0.0041498920036247s
END: 0.004223413998261094s

Python
END: 0.003860s
END: 0.004131s
END: 0.002629s

C
END: 0.000922s
END: 0.000938s
END: 0.000984s

Cが速いのは変わらずですが、テキストへのリダイレクトの場合はRubyの方が速かったのですが
端末へ標準出力させた場合はどちらも(Pythonのブレ幅は気になりますが)同じくらいという結果になりました。

まとめ

Rubyは遅くなかった。
  
  

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
5