Measure the execution result of the program in C ++, Java, Python.

Introduction

I wrote a benchmark program by myself in script languages such as C / C ++, Java / Scala, Python / Ruby / JavaScript, and discussed the result of its execution speed, so I will write it.

The program I wrote

For the time being, I made a program that outputs test only 100 ** 3 = 1000000 times.

C++

program

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

Execution result

Compiler: Apple LLVM version 7.3.0 (clang-703.0.31) elapsed time: 2.076[sec] Java

program

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

Execution result

Compiler: javac 1.8.0_74 elapsed time: 3.581[sec]

Python

program

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]"

Execution result

Version: Python 2.7.11 elapsed time: 2.25964999199[sec]

Recommended Posts

Measure the execution result of the program in C ++, Java, Python.
The result of Java engineers learning machine learning in Python www
The result of installing python in Anaconda
View the result of geometry processing in Python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
How to pass the execution result of a shell command in a list in Python
Measure function execution time in Python
linux / c> link> Get the execution result of the shell command in the C program> I was taught how to use popen ()
How to pass the execution result of a shell command in a list in Python (non-blocking version)
[Python] Let's reduce the number of elements in the result in set operations
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
[Python3] Define a decorator to measure the execution time of a function
Implement part of the process in C ++
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Set the process name of the Python program
I want to batch convert the result of "string" .split () in Python
I made a program to check the size of a file in Python
How to use the C library in Python
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
Prepare the execution environment of Python3 with Docker
Get the caller of a function in Python
Match the distribution of each group in Python
Calculation result after the decimal point in Python
Make a copy of the list in Python
Find the divisor of the value entered in python
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
Use the Java SDK of GoogleMapsAPI to get the result of reverse GeoCoding in Japanese.
the zen of Python
Next Python in C
C API in Python 3
Experience the good calculation efficiency of vectorization in Python
[Python] A program that counts the number of valleys
How to get the number of digits in Python
I want to grep the execution result of strace
How to measure processing time in Python or Java
Output the time from the time the program was started in python
[python] Get the list of classes defined in the module
Try embedding Python in a C ++ program with pybind11
Python points from the perspective of a C programmer
Ruby, Python code fragment execution of selection in Emacs
The story of FileNotFound in Python open () mode ='w'
Get the result in dict format with Python psycopg2
Learn the design pattern "Chain of Responsibility" in Python
Implement the solution of Riccati algebraic equations in Python
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
Let's use the open data of "Mamebus" in Python
Check for the existence of BigQuery tables in Java
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
[Python] Outputs all combinations of elements in the list
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python