Skip to content

Instantly share code, notes, and snippets.

Created April 29, 2014 04:44
Show Gist options
  • Save anonymous/11390832 to your computer and use it in GitHub Desktop.
Save anonymous/11390832 to your computer and use it in GitHub Desktop.
split in ruby
results from time:
# ruby
% time ruby split.rb
real 1m18.252s
user 1m16.855s
sys 0m0.979s
=============
# nimrod
% time ./split
real 2m39.531s
user 2m38.931s
sys 0m0.480s
# c
% time ./splitter
real 0m13.872s
user 0m13.562s
sys 0m0.300s
import strutils
let fn = "junkdata.txt"
for line in lines(fn):
let
ln = line.split("\t")
data1 = ln[0]
data2 = ln[1]
#!/usr/bin/env ruby
# create file if not exist.
fn = "junkdata.txt"
if not File.exist?(fn)
File.open("junkdata.txt","w") do |f|
100_000_000.times { f.puts "this is data field number1\tV" }
end
puts "file created. run again to parse. exitting"
exit
end
File.open(fn,"rb").each do |line|
line.chomp!
data1,data2 = line.split("\t")
end
#include <stdio.h>
int main() {
char data1[70];
char data2[2];
char line[100];
int j = 0;
FILE *fp = fopen("junkdata.txt", "rb");
if (!fp) {
printf("Couldn't open file for reading\n");
return 0;
}
while(fgets(line, 99, fp) != NULL){
sscanf(line, "%s\t%s\n", data1, data2);
j++;
/*
if(0 && j < 100) {
printf("%d) %s -> %s\n", j,data1,data2);
}
*/
}
printf("%d\n",j);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment