Skip to content

Instantly share code, notes, and snippets.

@philippbayer
Created April 10, 2012 10:04
Show Gist options
  • Save philippbayer/2350045 to your computer and use it in GitHub Desktop.
Save philippbayer/2350045 to your computer and use it in GitHub Desktop.
All languages.
-------------------------------------------------------
C++:
#include <iostream>
#include <string>
#include <fstream>
#include <boost/tokenizer.hpp>
#include <vector>
#include <iterator>
#include <sys/time.h>
using namespace std;
using namespace boost;
int main()
{
string data = ("test.csv");
struct timeval begin, end;
gettimeofday(&begin, NULL);
ifstream in(data.c_str());
typedef tokenizer< escaped_list_separator<char> > Tokenizer;
string line;
while(getline(in, line))
{
Tokenizer tok(line);
}
gettimeofday(&end, NULL);
cout << "Time elapsed: " << (begin.tv_usec - end.tv_usec)/1000 <<endl;
}
-------------------------------------------------------
D:
import std.file;
import std.string;
void main(string args[]) {
char[][][] f = split(split(cast(char[])read("test.csv"), "\n"), "\t");
}
-------------------------------------------------------
Julia:
macro timeit(ex,name)
quote
t = Inf
for i=1:5
t = min(t, @elapsed $ex)
end
println(t*1000)
end
end
function parse()
file = LineIterator(open("./test.csv"))
for line in file
split(line, '\t')
end
end
@timeit parse() "parse"
-------------------------------------------------------
Perl:
use Time::HiRes qw(gettimeofday);
my $t = gettimeofday();
open(FILE, './2.txt');
while( <FILE> ) {
split('\t');
}
close (FILE);
print(gettimeofday()-$t . "\n");
exit;
---------------------------------------------------------
Python:
#!/usr/bin/python
import time
def parse():
file_handle = open("./test.csv")
for line in file_handle:
line.split("\t")
tmin = float("inf")
for i in range(5):
t = time.time()
parse()
t = time.time()-t
if t < tmin:
tmin = t
print(str(tmin))
---------------------------------------------------------
Ruby:
min = 1000000000
5.times do
t = Time.now
File.open("./test.csv").each do |r|
r.split("\t")
end
finish = (Time.now - t)
if finish < min
min = finish
end
end
puts min
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment