← Back to context

Comment by za3faran

3 hours ago

It is not a static string, the `param` argument gets passed in each time. I modified the above benchmark to add an int parameter in addition to the `param` argument from before. However now it's testing an itoa as well as it is dependent on the number of iterations the benchmark suite decides to run, so it is not as precise, but Java is still ahead.

Java:

    @Param({"string"})
    public String param;

    public int i = 0;

    @Benchmark
    public String concat() {
         return "prefix " + param + " " + i++ + " suffix";
     }

    Benchmark          (param)  Mode  Cnt   Score   Error  Units
    Benchmarks.concat   string  avgt   15  26.591 ± 0.242  ns/op

Rust:

    fn format2(state: &mut BenchState) -> String {
        let i = state.next_i();

        format!("prefix {} {} suffix", state.param, i)
    }


    format2                 time:   [51.923 ns 52.541 ns 53.466 ns]
    Found 10 outliers among 100 measurements (10.00%)
    4 (4.00%) high mild
    6 (6.00%) high severe