Skip to content

Commit b98687c

Browse files
committed
Benchmark & Bug fixed
1 parent c5f1f45 commit b98687c

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

.github/workflows/ocaml.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ jobs:
5555
- name: Build
5656
run: opam exec -- dune build ./sppak.exe --release
5757

58+
- name: Benchmark
59+
run: opam exec -- dune runtest --release
60+
5861
- name: Rename for Unix-like platform
5962
if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
6063
run: mv ./_build/default/sppak.exe ./_build/default/sppak

bench.ml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
open Bin_pack;;
2+
open Printf;;
3+
4+
5+
let test test_name test_case =
6+
let rects =
7+
test_case
8+
|> List.concat_map (fun (w, h, count) ->
9+
List.init count (fun _ ->
10+
{ w = w; h = h; tag = () })) in
11+
12+
let start_time = Sys.time () in
13+
let result = bin_pack rects in
14+
let time_span = Sys.time () -. start_time in
15+
let all_area = result.width * result.height in
16+
let filled_area = sum_by (fun (r, _) -> r.w * r.h) result.rects in
17+
let fill_rate = float_of_int filled_area /. float_of_int all_area in
18+
19+
printf
20+
"rects:%3d\tfill-rate:%f%%\ttime:%fs\t in %s\n"
21+
(List.length rects)
22+
(fill_rate *. 100.0)
23+
time_span
24+
test_name
25+
;;
26+
27+
28+
let () =
29+
test "simple"
30+
[ 500, 200, 1;
31+
250, 200, 1;
32+
50, 50, 20 ];
33+
34+
test "tall"
35+
[ 50, 400, 2;
36+
50, 300, 5;
37+
50, 200, 10;
38+
50, 100, 20;
39+
50, 50, 40 ];
40+
41+
test "wide"
42+
[ 400, 50, 2;
43+
300, 50, 5;
44+
200, 50, 10;
45+
100, 50, 20;
46+
50, 50, 40 ];
47+
48+
test "tallAndWide"
49+
[ 100, 400, 3;
50+
400, 100, 3 ];
51+
52+
test "powersOf2"
53+
[ 2, 2, 256;
54+
4, 4, 128;
55+
8, 8, 64;
56+
16, 16, 32;
57+
32, 32, 16;
58+
64, 64, 8;
59+
128, 128, 4;
60+
256, 256, 2 ];
61+
62+
test "oddAndEven"
63+
[ 50, 50, 20;
64+
47, 31, 20;
65+
23, 17, 20;
66+
109, 42, 20;
67+
42, 109, 20;
68+
17, 33, 20 ];
69+
70+
test "complex"
71+
[ 100, 100, 3;
72+
60, 60, 3;
73+
50, 20, 20;
74+
20, 50, 20;
75+
250, 250, 1;
76+
250, 100, 1;
77+
100, 250, 1;
78+
400, 80, 1;
79+
80, 400, 1;
80+
10, 10, 100;
81+
5, 5, 500 ];
82+
83+
test "superComplex"
84+
[ 100, 100, 30;
85+
60, 60, 30;
86+
50, 20, 200;
87+
20, 50, 200;
88+
250, 250, 10;
89+
250, 100, 10;
90+
100, 250, 10;
91+
400, 80, 10;
92+
80, 400, 10;
93+
10, 10, 1000;
94+
5, 5, 5000 ];
95+
96+
test "superSuperComplex"
97+
[ 100, 100, 300;
98+
60, 60, 300;
99+
50, 20, 2000;
100+
20, 50, 2000;
101+
250, 250, 100;
102+
250, 100, 100;
103+
100, 250, 100;
104+
400, 80, 100;
105+
80, 400, 100;
106+
10, 10, 10000;
107+
5, 5, 50000 ]
108+

bin_pack.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let rec except_one x =
2929

3030

3131
let sum_by by ls =
32-
List.map by ls
32+
(0 :: List.map by ls)
3333
|> reduce_left (fun a b -> a + b)
3434
;;
3535

dune

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
(library
2+
(name bin_pack)
3+
(modules Bin_pack))
4+
15
(executable
26
(name sppak)
3-
(libraries stb_image stb_image_write))
7+
(modules Sppak)
8+
(libraries stb_image stb_image_write bin_pack))
9+
10+
(test
11+
(name bench)
12+
(modules Bench)
13+
(libraries bin_pack))
14+

0 commit comments

Comments
 (0)