simple_parsing.ml :
parsing_scanbuf.ml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 module Color = struct let htbl = Hashtbl.create 7 let of_rgb (r, g, b) = r lsl 16 + g lsl 8 + b let to_rgb d = (d lsr 16, (d lsr 8) land 0xFF, d land 0xFF) let add color = Hashtbl.add htbl (of_rgb color) () let mem color = Hashtbl.mem htbl (of_rgb color) let rem color = Hashtbl.remove htbl (of_rgb color) let results() = Hashtbl.fold (fun color _ acc -> (to_rgb color)::acc) htbl [] end let parse str = Scanf.sscanf str "%d %d %d" (fun r g b -> (r,g,b)) ;; let input_line ic = try Some(input_line ic) with End_of_file -> None let () = let ic = open_in "foo.txt" in let rec read_loop () = match input_line ic with | Some line -> let color = parse line in if not(Color.mem color) then Color.add color; read_loop () | None -> close_in ic; (Color.results()) in let colors_list = read_loop () in List.iter (fun (r,g,b) -> Printf.printf "%d %d %d\n" r g b) colors_list; ;;
no_parsing.ml (hashing full strings) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 module Color = struct let htbl = Hashtbl.create 7 let of_rgb (r, g, b) = r lsl 16 + g lsl 8 + b let to_rgb d = (d lsr 16, (d lsr 8) land 0xFF, d land 0xFF) let add color = Hashtbl.add htbl (of_rgb color) () let mem color = Hashtbl.mem htbl (of_rgb color) let rem color = Hashtbl.remove htbl (of_rgb color) let results() = Hashtbl.fold (fun color _ acc -> (to_rgb color)::acc) htbl [] end let p_scanbuf ~f = let buf = Scanf.Scanning.from_file "/tmp/foo.txt" in let rec loop () = if Scanf.Scanning.end_of_input buf then (Color.results()) else Scanf.bscanf buf " %d %d %d " (fun r g b -> f (r, g, b); loop()) in loop () let () = let f color = if not(Color.mem color) then Color.add color; in let colors_list = p_scanbuf ~f in List.iter (fun (r,g,b) -> Printf.printf "%d %d %d\n" r g b) colors_list; ;;
résultats obtenus avec la commande time :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 module Color = struct let htbl = Hashtbl.create 7 let add color = Hashtbl.add htbl color () let mem color = Hashtbl.mem htbl color let rem color = Hashtbl.remove htbl color let results() = Hashtbl.fold (fun color _ acc -> color::acc) htbl [] end let parse str = Scanf.sscanf str "%d %d %d" (fun r g b -> (r,g,b)) ;; let input_line ic = try Some(input_line ic) with End_of_file -> None let () = let ic = open_in "foo.txt" in let rec read_loop () = match input_line ic with | Some color -> if not(Color.mem color) then Color.add color; read_loop () | None -> close_in ic; (Color.results()) in let colors_list = List.map parse (read_loop()) in List.iter (fun (r,g,b) -> Printf.printf "%d %d %d\n" r g b) colors_list; ;;
Avec Scanf.Scanning.from_file on peut effectivement gagner un peu comme tu le prédisais, cependant on est encore loin de la version sans parsing du tout.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 simple_parsing.opt -- 01.67 elapsed parsing_scanbuf.opt -- 01.24 elapsed no_parsing.opt -- 00.30 elapsed
C'est moi qui gagne
Partager