Monday, May 4, 2020

HASH searches.

# 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 #include #include #include #include "hashmap.h" #define KEY_MAX_LENGTH (256) #define KEY_PREFIX ("somekey") typedef struct data_struct_s { char key_string[KEY_MAX_LENGTH]; int number; } data_struct_t; int main() { int error; map_t mymap; data_struct_t* value; mymap = hashmap_new(); char key_string[KEY_MAX_LENGTH]; FILE *file = fopen("150k-lines", "r"); FILE *fileOne = fopen("one-mil-lines", "r"); int KEY_COUNT = 0; char lineinF[256]; while(fgets(lineinF, sizeof lineinF, fileOne)!=NULL) { if(lineinF[0] != '\n') { value = malloc(sizeof(data_struct_t)); lineinF[strlen(lineinF) - 1] = '\0'; snprintf(value->key_string, KEY_MAX_LENGTH, "%s", lineinF); value->number = KEY_COUNT; error = hashmap_put(mymap, value->key_string, value); assert(error==MAP_OK); KEY_COUNT++; } } fclose(fileOne); //printf("%d\n",KEY_COUNT); //fclose(file); //hashmap_free(mymap); //return 0; while(fgets(lineinF, sizeof lineinF, file)!=NULL) { if(lineinF[0] != '\n') { lineinF[strlen(lineinF) - 1] = '\0'; snprintf(key_string, KEY_MAX_LENGTH, "%s", lineinF); error = hashmap_get(mymap, key_string, (void**)(&value)); if(error == 0) printf("OK - %s\n",lineinF); else printf("NOT OK - %s\n",lineinF); } } fclose(file); /* Destroy the map */ hashmap_free(mymap); return 1; }

1 comment:

  1. Best Casinos Near Me - MapyRO
    Best Casinos Near Me - Find 양주 출장샵 Casinos Near Me 삼척 출장안마 Near You 강원도 출장안마 on MapyRO. 동해 출장마사지 경상남도 출장안마 casinos that offer slot machines (slots) and table games.

    ReplyDelete