main.s
.global _start
.section .text
_start:
movq (%rsp), %rax
cmp $1, %rax
jle _args_fail
#open
movq $2, %rax #syscall code
movq 16(%rsp), %rdi#argv[1]
xor %rsi, %rsi #read only
xor %rdx, %rdx #no mode
syscall
movq %rax, %r15
#fstat
movq $5, %rax
movq %r15, %rdi #fd
leaq .statbuf(%rip), %rsi
syscall
#mmap
movq $9, %rax
xor %rdi, %rdi
movq .statbuf+48(%rip), %rsi #length
movq $7, %rdx #read/write/exec
movq $2, %r10 #private
movq %r15, %r8 #fd
movq $0, %r9
syscall
movq %rax, %r14 #start of alloc
movq %rax, %r13
add .statbuf+48(%rip), %r13 # end of alloc
sub $5, %r13
movq $0, %rax #score
movq $0, %r8
movq $0, %r9
movq $0, %r10
movq $0, %r11
_start_of_line:
cmp %r14, %r13
jl _end
movb (%r14), %r8b # start1
addq $2, %r14
movb (%r14), %r9b # end1
addq $2, %r14
movb (%r14), %r10b# start2
addq $2, %r14
movb (%r14), %r11b# end2
addq $2, %r14
_chk_1_inc_2:
cmp %r10, %r8 #if a>b
jg _chk_2_inc_1 #no
cmp %r11, %r9 #if a<b
jge _add_score #yes
_chk_2_inc_1:
cmp %r8, %r10 #check start2 > start1
jg _start_of_line #no # if start2 > start1
cmp %r9, %r11 #if end2 > end1
jl _start_of_line #no if end2 < end1
_add_score:
addq $1, %rax
jmp _start_of_line
_end:
leaq .printfmt(%rip), %rdi
movq %rax, %rsi
call printf@PLT
movq $60, %rax
movq $0, %rdi
syscall
_args_fail:
leaq .args_fail_msg(%rip), %rdi
call puts@PLT
movq $60, %rax
movq $1, %rdi
syscall
.section .data
.args_fail_msg:
.asciz "you must provide the filename to check"
.printfmt:
.asciz "%d\n"
.section .bss
.lcomm .statbuf 144
main.c
#include<unistd.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<stdio.h>
#include<fcntl.h>
int main(int argc, char** argv){
if(argc == 1){
puts("You must provide a filename");
return -1;
}
struct stat statbuf;
int fd = open(argv[1],0,0);
fstat(fd,&statbuf);
char* f_data = mmap(0,statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
int total = 0;
int i = 0;
while(i < (statbuf.st_size-5)){
char start1 = f_data[i];
char end1 = f_data[i+2];
char start2 = f_data[i+4];
char end2 = f_data[i+6];
if(start1>=start2)
if(end1<=end2)
goto add_score;
if(start2>=start1)
if(end2<=end1)
goto add_score;
i+=8;
continue;
add_score:
total++;
i+=8;
}
printf("%d\n",total);
}
main.rs
use std::fs;
use std::env;
fn main(){
let f_cont = fs::read_to_string(env::args().nth(1).unwrap()).expect("no file");
let mut total_score: u64 = 0;
for line in f_cont.split("\n") {
let line = line.as_bytes();
if line.len() < 6 {
continue;
}
let start1 = line[0];
let end1 = line[2];
let start2 = line[4];
let end2 = line[6];
if start1<=start2 {
if end1>=end2 {
total_score+=1;
continue;
}
}
if start2<=start1 {
if end2>=end1 {
total_score+=1;
continue;
}
}
}
println!("{}", total_score);
}
main.py
#!/usr/bin/env python3
import sys
file = open(sys.argv[1]).read()
lines = [i.strip() for i in file.split('\n') if len(i.strip()) > 0]
total_score = 0
for site in lines:
site=site.split(",")
site0 = site[0].split("-")
site1 = site[1].split("-")
site0 = set(range( int(site0[0]), int(site0[1])+1))
site1 = set(range( int(site1[0]), int(site1[1])+1))
intersection = site0.intersection(site1)
if( intersection == site0 or intersection == site1):
total_score+=1
print (total_score)
main.rb
#!/usr/bin/env ruby
total_score = 0
File.foreach(ARGV[0]) { |line|
if line.length == 0
next
end
line = line.split(",")
r1 = line[0].split("-")
r2 = line[1].split("-")
r1 = (Integer(r1[0]) .. Integer(r1[1])).to_a
r2 = (Integer(r2[0]) .. Integer(r2[1])).to_a
if (r1-r2).empty?
total_score+=1
elsif (r2-r1).empty?
total_score+=1
end
}
puts total_score
main.hs
import System.IO
import System.Environment
import Data.List.Split
import Data.List
import Debug.Trace
main = do
args <- getArgs
handle <- openFile(args !! 0) ReadMode
content <- hGetContents handle
print $ getTotalScore $ lines content
getTotalScore :: [String] -> Int
getTotalScore lines = sum (map getLineScore lines)
getLineScore :: String -> Int
getLineScore line = do
let sp = splitOn "," line
let r1 = getRange (sp !! 0)
let r2 = getRange (sp !! 1)
if (isInfixOf r1 r2) || (isInfixOf r2 r1) then 1 else 0
getRange :: String -> [Int]
getRange str = do
let s_str = splitOn "-" str
let x = read (s_str !! 0) :: Int
let y = read (s_str !! 1) :: Int
[x..y]
times.txt
time it took for each file to run on a 500mb file (50 million lines)
main.s: 0.527s
main.c: 0.872s
main.rs: 9.022s
main.py: 2m9.268s
main.rb: 3m31.754s
main.hs: 8m56.989s