About Me

My photo
Author of Groovy modules: GBench, GProf, Co-author of a Java book パーフェクトJava, Game Developer at GREE

Saturday, June 25, 2011

GBench = @Benchmark Annotation + BenchmarkBuilder

I released GBench, a benchmarking framework for Groovy. Now GBench has two features, @Benchmark Annotation and BenchmarkBuilder. @Benchmark Annotation is an annotation that allows to measure execution time of methods without modifying production code and is already published. Please read the previous post for more detail information. BenchmarkBuilder is a convenient builder to write benchmark code easily and now appears for the first time.


The following code is an example to get benchmark string concatenation by repeating them 1000 times:
----
def strings = ['GBench', ' = ', '@Benchmark Annotation', ' + ', 'BenchmarkBuilder']
def benchmark = new BenchmarkBuilder()
benchmark.run times: 1000, {
    with '+=', { 
        def s = ''
        for (string in strings) {
            s += string    
        }
    }
    with 'concat', { 
        def s = ''
        for (string in strings) {
            s.concat string    
        }
    }
    with 'string builder', {
        def sb = new StringBuilder()    
        for (string in strings) {
            sb << string    
        }
        sb.toString() 
    }
    with 'join', {
        strings.join()
    }
}
println benchmark
----


The output will be like this:
----
                 time
+=             18197705
concat         7669621
string builder 9420539
join           5248348
----

Of course you can sort the results:
----
println benchmark.sort({ lhs, rhs -> lhs.time <=> rhs.time })
----


----
                 time
join            5248348
concat          7669621
string builder  9420539
+=             18197705
----

and also you can handle the results as you want!:
----
new File('benchmark.csv').withWriter { file ->
    file.writeLine 'label,time(ms)'
    benchmark.sort().each { bm ->
        file.writeLine "${bm.label},${bm.time / 1000000}"
    }
}
----


----
> cat benchmark.csv
label,time(ms)
join,5.248348
concat,7.669621
string builder,9.420539
+=,18.197705
----


For now, GBench can measure only wall-clock time but I'm planning to also support CPU time and user time in a future release.


You can download GBench from here. Please try and let me know your feedback!

1 comment:

  1. TITanium ARCH VR Headsets - TITanium.com
    TITanium ARCH VR Headsets - TITanium.com offers a list of T-Bone joico titanium Exercise womens titanium wedding bands games, titanium ingot and a full list of the best 2014 ford focus titanium hatchback T-Bone VR headsets aftershokz trekz titanium for

    ReplyDelete