/* * [TestFNV1a32.java] * * Summary: Test FNV1a (Fowler/Noll/Vo) 32-bit hashing function. * * Copyright: (c) 2014-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2014-04-08 initial version */ package com.mindprod.example; import com.mindprod.common18.EIO; import java.io.UnsupportedEncodingException; import static java.lang.System.*; /** * Test FNV1a (Fowler/Noll/Vo) 32-bit hashing function. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2014-04-08 initial version * @since 2014-04-08 */ public class TestFNV1a32 { ; /** * magic 32 bit initial value for the hash = 2,166,136,261 */ private static final int MAGIC_OFFSET = ( int ) ( 2_166_136_261L & 0xffffffffL ); /* fake unsigned 32 bit */ /** * prime specified to use in 32 bit hashes */ private static final int MAGIC_PRIME = 16_777_619; /* magic 32 bit FNV_prime = 2^24 + 2^8 + 0x93 = 16,777,619 */ /** * Calc 32 bit FNV1a digest of an array of bytes. * * @param theTextToDigestAsBytes byte array to compute checksum on * * @return 32-bit signed fnv1a checksum */ private static int fnv1a32( byte[] theTextToDigestAsBytes ) { int hash = MAGIC_OFFSET; for ( byte b : theTextToDigestAsBytes ) { hash ^= b; hash *= MAGIC_PRIME; } return hash; } /** * Test harness * see http://isthe.com/chongo/tech/comp/fnv/#FNV-1a * * @param args not used * * @throws java.io.UnsupportedEncodingException in case UTF-8 support is missing. */ public static void main( String[] args ) throws UnsupportedEncodingException { final String s = "The quick brown fox jumped over the lazy dog's back; sample url: http://mindprod.com/jgloss/digest.html"; final byte[] theTextToDigestAsBytes = s.getBytes( EIO.UTF8 ); out.println( fnv1a32( theTextToDigestAsBytes ) ); long start = System.nanoTime(); for ( int i = 0; i < 100_000; i++ ) { fnv1a32( theTextToDigestAsBytes ); } long stop = System.nanoTime(); out.println( ( stop - start ) / 1_000_000L + " seconds to compute 100,000 FNV1a32 hashes" ); // On my machine it takes: 20 seconds to compute 100,000 FNV1a64 hashes } }