benchmarks/string: Added benchmark for Array.to_s
[nit.git] / benchmarks / strings / array_tos.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Benchmark on the Array.to_s function using several methods.
12 module array_tos
13
14 import opts
15
16 fun bench_array(arr_size, item_size, loops: Int)
17 do
18 var arr = new Array[String].with_capacity(arr_size)
19 var s = "a" * item_size
20
21 for i in [0 .. arr_size[ do arr.push s
22
23 for i in [0..loops[ do
24 s = arr.to_s
25 end
26 end
27
28 var opts = new OptionContext
29 var nb_ccts = new OptionInt("Size of an element", -1, "--ccts")
30 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
31 var strlen = new OptionInt("Length of the Array", -1, "--strlen")
32 opts.add_option(nb_ccts, loops, strlen)
33
34 opts.parse(args)
35
36 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
37 opts.usage
38 exit(-1)
39 end
40
41 bench_array(strlen.value, nb_ccts.value, loops.value)