はじめに
自分でC/C++, Java/Scala, Python/Ruby/JavaScriptなどのスクリプト言語でそれぞれベンチマークプログラムを書き,その実行速度の結果を議論せよという課題が出たので書いてみる.
書いたプログラム
とりあえず全て 100**3 = 1000000回だけ test と出力するようなプログラムにした.
C++
プログラム
benchmark.cpp
# include <iostream>
# include <chrono>
using namespace std;
void benchmark();
int main() {
  std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
  benchmark();
  std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
  std::cout << "elapsed time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() /1000.0 << "[sec]\n";
  return 0;
}
void benchmark() {
  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++)
      for (int k = 0; k < 100; k++)
        std::cout << "test\n";
}
実行結果
コンパイラ: Apple LLVM version 7.3.0 (clang-703.0.31)
elapsed time: 2.076[sec]
Java
プログラム
benchmark.java
public class benchmark {
  public static void main (String[] args) {
    long start = System.currentTimeMillis();
    benchmark();
    long end = System.currentTimeMillis();
    System.out.println("elapsed time: " + ((end - start) / 1000.0) + "[sec]");
  }
  private static void benchmark() {
    for (int i = 0; i < 100; i++)
      for (int j = 0; j < 100; j++)
        for (int k = 0; k < 100; k++)
          System.out.println("test");
  }
}
実行結果
コンパイラ: javac 1.8.0_74
elapsed time: 3.581[sec]
Python
プログラム
benchmark.py
import time
def benchmark():
    for i in xrange(100):
        for j in xrange(100):
            for k in xrange(100):
                print 'test'
if __name__ == '__main__':
    start = time.time()
    benchmark()
    end = time.time()
    print ("elapsed time: {0}".format(end - start)) + "[sec]"
実行結果
バージョン: Python 2.7.11
elapsed time: 2.25964999199[sec]