aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/developernotes/Utf8MicroBencmark.java
blob: ac1e26732cfd4d843b092571cacc2553139fb591 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

/**
 * @author baldersheim
 * @since 5.2
 */

public class Utf8MicroBencmark {
    public void benchmark(int sizeInK) {
        String [] l = new String[1000];
        for (int i=0; i < l.length; i++) {
            l[i] = "typical ascii string" + i;
        }
        System.out.println("Warming up...");
        utf8encode(l, 10000); // warm-up
        utf8encodeFast(l, 10000);

        long startTime, endTime, sum;
        System.out.println("Starting benchmark ...");
        startTime=System.currentTimeMillis();
        sum = utf8encode(l, sizeInK);
        endTime=System.currentTimeMillis();
        System.out.println("Utf8 encoding " + sizeInK + "k strings took " + (endTime-startTime) + "ms generating " + sum + "bytes");
        startTime=System.currentTimeMillis();
        sum = utf8encodeFast(l, sizeInK);
        endTime=System.currentTimeMillis();
        System.out.println("Utf8 fast encoding " + sizeInK + "k strings took " + (endTime-startTime) + "ms generating " + sum + "bytes");
    }

    private long utf8encode(String [] l, int sizeInK) {
        long sum = 0;
        for (int i=0; i<1000*sizeInK; i++) {
            sum += Utf8.toBytesStd(l[i%l.length]).length;
        }
        return sum;
    }
    private long utf8encodeFast(String [] l, int sizeInK) {
        long sum = 0;
        for (int i=0; i<1000*sizeInK; i++) {
            sum += Utf8.toBytes(l[i%l.length]).length;
        }
        return sum;
    }

    public static void main(String[] args) {
        new Utf8MicroBencmark().benchmark(10000);
    }

}