Last active
August 29, 2015 13:55
-
-
Save l1x/8787970 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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