December
18th,
2012
Just for curiosity, I wanted to see how fast is C++ compared with regular Python and Python using the numpy library.
Below the results:
A bit of Python code with regular python, same loop using numpy, and numpy vectorial (like in MatLab):
For C++, I just did two cycles, one without initialising the vector with its size, and other one with it. Code is below:
The result is:
It's interesting to see how the programming approach changes the results!
Surprisingly, Python, using a vectorial numpy approach, can be as fast as C++. It's not a surprise however, that an initlised array in C++ can be twice faster!
So I tried first with ArrayList and Vector but they were so slow (more than 30 seconds), that I didn't even dare to add them below... I came up with this code, but doesn't really compare to what I did in C++.
Using regular arrays, Java can be as fast as C++ Vectors with predefined length.
A bit of Python code with regular python, same loop using numpy, and numpy vectorial (like in MatLab):
'''
Created on Nov 19, 2012
@author: leal
'''
import numpy as np
from datetime import datetime
nSpectra = 12 * 32 * 256def testSpectraIN5(): total = [] for i in range(nBinsTotal): res = 500 * 500 + i total.append(res) def testSpectraIN5np(): total = np.empty(nBinsTotal, dtype=float) for i in range(nBinsTotal): res = 500 * 500 + i total[i]=res def testSpectraIN5npOpt(): # res = [0..nBinsTotal-1] res = np.arange(nBinsTotal, dtype=float) total = 500 * 500 + res if __name__ == '__main__': print "Main has started!" print "* testSpectraIN5" t_start = datetime.now() testSpectraIN5() t_end = datetime.now() t_total = t_end - t_start print "Total time: ", t_total, " seconds" print "* testSpectraIN5np" t_start = datetime.now() testSpectraIN5np() t_end = datetime.now() t_total = t_end - t_start print "Total time: ", t_total, " seconds" print "* testSpectraIN5npOpt" t_start = datetime.now() testSpectraIN5npOpt() t_end = datetime.now() t_total = t_end - t_start print "Total time: ", t_total, " seconds" print "Main has finished!"
nBinsTotal = nSpectra * 512;
Python v 2.7.3 results:
Main has started!
* testSpectraIN5
Total time: 0:00:12.781883 seconds
* testSpectraIN5np
Total time: 0:00:09.553932 seconds
* testSpectraIN5npOpt
Total time: 0:00:00.823714 seconds
Main has finished!
For C++, I just did two cycles, one without initialising the vector with its size, and other one with it. Code is below:
// constructing vectors
#include <iostream>
#include <vector>
#include <valarray>
// C++11
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds milliseconds;
int main() {
std::cout << "Main has started!" << std::endl;
Clock::time_point t0 = Clock::now();
int nSpectra = 12 * 32 * 256;
int nBinsTotal = nSpectra * 512;
std::vector<double> total; //(nBinsTotal);
for (int i = 0; i < nBinsTotal; i++) {
double res = 500 * 500 + i;
total.push_back(res);
}
Clock::time_point t1 = Clock::now();
milliseconds ms = std::chrono::duration_cast<milliseconds>(t1 - t0);
std::cout.precision(2);
std::cout << "Total time: " << std::fixed << ms.count() * 1e-3 << " seconds"<< std::endl;
std::cout << "Vector with known size:"<< std::endl;
t0 = Clock::now();
std::vector<double> total2(nBinsTotal);
for (int i = 0; i < nBinsTotal; i++) {
double res = 500 * 500 + i;
total2[i] = res;
}
t1 = Clock::now();
ms = std::chrono::duration_cast<milliseconds>(t1 - t0);
std::cout.precision(2);
std::cout << "Total time: " << std::fixed << ms.count() * 1e-3 << " seconds"<< std::endl;
std::cout << "Main has finished!" << std::endl;
return 0;
}
The result is:
Main has started!
Total time: 1.70 seconds
Vector with known size:
Total time: 0.74 seconds
Main has finished!
It's interesting to see how the programming approach changes the results!
Surprisingly, Python, using a vectorial numpy approach, can be as fast as C++. It's not a surprise however, that an initlised array in C++ can be twice faster!
EDIT:
What about java???So I tried first with ArrayList and Vector but they were so slow (more than 30 seconds), that I didn't even dare to add them below... I came up with this code, but doesn't really compare to what I did in C++.
import java.util.Vector;
public class VectorTest {
public static void main(String[] args) {
int nSpectra = 12 * 32 * 256;
int nBinsTotal = nSpectra * 512;
double[] total2 = new double[nBinsTotal];
long startTime = System.nanoTime();
// <E> Element type of Vector e.g. String, Integer, Object ...
for (int i = 0; i < nBinsTotal; i++) {
double res = 500 * 500 + i;
total2[i] = res;
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("Total time: " + duration * 1e-9);
}
}
Start calculation with arrays and initilisation.
Total time: 0.073217508
Using regular arrays, Java can be as fast as C++ Vectors with predefined length.