tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: MD5Encoder.encode versus HexUtils.toHexString
Date Mon, 17 Jun 2013 16:34:55 GMT
Chris,

On 6/17/13 11:38 AM, Christopher Schultz wrote:
>> Mark Thomas wrote
>>> I haven't actually written any performance tests, but looking at the
>>> code it seems that HexUtils.toHexString would execute slightly faster
>>> for a 16-byte array because of repeated integer multiplication in the loop.
>>
>> I'd want to see some hard numbers on both methods before making are
>> decisions based on performance.
> 
> I'll write some micro benchmarks and post the results.

Here's what I got for 10 runs in the same JVM run. Code at the bottom.
Looks like a dead heat, but I got all kinds of data all over the
place... sometimes HexUtils really beats MD5Encoder badly and sometimes
the opposite.

MD5Encoder	HexUtil
495ms	409ms
397ms	396ms
401ms	401ms
401ms	398ms
403ms	401ms
398ms	400ms
400ms	405ms
402ms	401ms
398ms	413ms
400ms	395ms

I'll see if I can tweak each of the implementations -- I think there is
an improvement or two that can be made.

-chris

PS the code:

import org.apache.catalina.util.MD5Encoder;
import org.apache.tomcat.util.buf.HexUtils;

public class DigestEncoderPerfTest
{
    static MD5Encoder md5encoder = new MD5Encoder();

    public static void main(String[] args)
        throws Exception
    {
        final byte[] testBytes = "thisisatestABCDE".getBytes("US-ASCII");
        final long iterations = 10000000;
        final int times = 10;

        // Warm-up the JIT
        testMD5Encoder(10, testBytes);
        testHexEncoder(10, testBytes);
        testMD5Encoder(10, testBytes);
        testHexEncoder(10, testBytes);
        testMD5Encoder(10, testBytes);
        testHexEncoder(10, testBytes);

        long elapsed;

        System.out.println("MD5Encoder\tHexUtil");

        for(int i=0; i<times; ++i)
        {
            elapsed = System.currentTimeMillis();
            testMD5Encoder(iterations, testBytes);
            elapsed = System.currentTimeMillis() - elapsed;

            System.out.print(elapsed + "ms");

            elapsed = System.currentTimeMillis();
            testMD5Encoder(iterations, testBytes);
            elapsed = System.currentTimeMillis() - elapsed;

            System.out.println("\t" + elapsed + "ms");
        }
    }

    public static void testMD5Encoder(final long iterations,
                                      final byte[] b)
    {
        for(long i = 0; i < iterations; ++i)
            md5encoder.encode(b);
    }
    public static void testHexEncoder(final long iterations,
                                      final byte[] b)
    {
        for(long i = 0; i < iterations; ++i)
            HexUtils.toHexString(b);
    }
}


Mime
View raw message