Skip to content

Instantly share code, notes, and snippets.

@l1x
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l1x/8787970 to your computer and use it in GitHub Desktop.
Save l1x/8787970 to your computer and use it in GitHub Desktop.
(ns clj-hashing
(:import
[com.google.common.hash Hashing HashFunction HashCode ]
[java.nio.charset Charset ]))
(def ^:private ^HashFunction murmur-fun (Hashing/murmur3_128))
(def ^:private ^HashFunction sha1-fun (Hashing/sha1))
(def ^:private ^HashFunction md5-fun (Hashing/md5))
(def ^:private ^sun.nio.cs.UTF_8 utf8-chr-set (Charset/forName "UTF-8"))
(defn gen-hash-no-type
"General generator for hashing functions"
[fun]
(fn [string]
(.toString (.hashString fun string utf8-chr-set))))
(def murmur2 (gen-hash-no-type murmur-fun))
(defn gen-hash
"General generator for hashing functions"
[^HashFunction fun]
(fn ^String [^String string]
(.toString (.hashString fun string utf8-chr-set))))
(def murmur (gen-hash murmur-fun))
(def sha1 (gen-hash sha1-fun))
(def md5 (gen-hash md5-fun))
;clj-hashing=> (time (count (map murmur (repeat 10000 "asdf"))))
;"Elapsed time: 8.523 msecs"
;10000
;clj-hashing=> (time (count (map murmur2 (repeat 10000 "asdf"))))
;"Elapsed time: 48.583 msecs"
;10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment