[PYTHON] Benchmark simple script

It was rumored that Ruby was slow, so I made a script for benchmarking and experimented.

This is a script that outputs 1000 lines of pseudo-random numbers.

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"

result

END: 0.016832s

2015/03/10 postscript A script that corrects what @scivola pointed out 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)

result

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);
}

result

END: 0.000017s

I feel that the ratio or the order is almost the same as the data that comes out by google. .. .. Ruby has beautiful code, so I think it's best suited for creating simple web applications and studying programming!

Yes.

Re-experiment

2015/03/15 postscript It seems that it was not necessary to put randam.rand (1000) in the block The process is also considerably faster, resulting in overtaking Python. (^ ^)

It is a mystery that the increment was added (´ω `) ‥ Toho

However‥

If it is a benchmark that only generates random numbers, the language processing system is not so relevant, I think it will measure the performance of the built-in random number generator.

Certainly. I feel like that. That's why I conducted a test that also outputs. Ruby uses Puts because it breaks lines according to Python's print and C's printf.    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 compiler

#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

Since it counts the number of lines, I tried to insert the process of outputting to text by redirect. It is the result of not making standard output. Result is C、Ruby、Python The result is that it is faster in the order of.

Inside story

Write the result of standard output to the terminal, not the output to tail or text. 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 is still faster, but Ruby was faster when redirecting to text. When standard output was made to the terminal, the result was about the same (although I'm worried about the blur width of Python).

Summary

Ruby wasn't slow.

Recommended Posts

Benchmark simple script
A simple IDAPython script to name a function