# Search 150K lines(in a file) from 1 million lines (in a file)
BASH (N/A) |
---|
N/A |
PYTHON (2 seconds) |
# Sets are using hash. #!/bin/python with open('150k-lines') as file1, open('one-mil-lines') as file2: a_lines = set(file1.read().splitlines()) b_lines = set(file2.read().splitlines()) for line in a_lines: if not line in b_lines: print "NOT OK %s" %(line) else: print "OK %s" %(line) |
GOLANG (2.95 seconds) |
package main import ( "bufio" "fmt" "os" "log" ) func main() { readFile1, err := os.Open("150k-lines") readFile2, err := os.Open("one-mil-lines") // readFile1, err := os.Open("testfile1") // readFile2, err := os.Open("testfile2") if err != nil { log.Fatalf("failed to open file: %s", err) } fileScanner1 := bufio.NewScanner(readFile1) fileScanner2 := bufio.NewScanner(readFile2) fileScanner1.Split(bufio.ScanLines) fileScanner2.Split(bufio.ScanLines) var fileTextLines1, fileTextLines2 []string var key string var value int var ok bool hash1 := make(map[string]int) hash2 := make(map[string]int) linenum1 := 1 linenum2 := 1 for fileScanner1.Scan() { fileTextLines1 = append(fileTextLines1, fileScanner1.Text()) hash1[fileScanner1.Text()] = linenum1 linenum1++ } for fileScanner2.Scan() { fileTextLines2 = append(fileTextLines2, fileScanner2.Text()) hash2[fileScanner2.Text()] = linenum2 linenum2++ } readFile1.Close() readFile2.Close() for key, value = range hash1 { if value, ok = hash2[key]; ok { fmt.Println(key, value) } } } |
C (3.45 seconds) |
/*
* You must download hashmap.h and hashmap.c from THIS LINK
*/
#include |