Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: addthis/stream-lib
...
head fork: addthis/stream-lib
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
Commits on Jul 12, 2012
@jkff jkff Dramatically faster MurmurHash for primitive types
 (TestHyperLogLog down from 100+s to 8.5s which are mostly random number generation)
7fc6f9f
Commits on Jul 27, 2012
@cburroughs cburroughs Merge remote-tracking branch 'jkff/faster-murmur-hash'
Conflicts:
	src/main/java/com/clearspring/analytics/stream/cardinality/LinearCounting.java
db2ede7
View
40 src/main/java/com/clearspring/analytics/hash/MurmurHash.java
@@ -28,6 +28,23 @@
*/
public class MurmurHash
{
+ public static int hash(Object o)
+ {
+ if (o == null)
+ return 0;
+ if(o instanceof Long)
+ return hashLong((Long)o);
+ if(o instanceof Integer)
+ return hashLong((Integer)o);
+ if(o instanceof Double)
+ return hashLong(Double.doubleToRawLongBits((Double)o));
+ if(o instanceof Float)
+ return hashLong(Float.floatToRawIntBits((Float)o));
+ if(o instanceof String)
+ return hash(((String)o).getBytes());
+ return hash(o.toString());
+ }
+
public static int hash(byte[] data)
{
return hash(data, data.length, -1);
@@ -92,4 +109,27 @@ public static int hash(byte[] data, int length, int seed)
return h;
}
+
+ public static int hashLong(long data)
+ {
+ int m = 0x5bd1e995;
+ int r = 24;
+
+ int h = 0;
+
+ int k = (int)data * m;
+ k ^= k >>> r;
+ h ^= k*m;
+
+ k = (int)(data>>32) * m;
+ k ^= k >>> r;
+ h *= m;
+ h ^= k*m;
+
+ h ^= h >>> 13;
+ h *= m;
+ h ^= h >>> 15;
+
+ return h;
+ }
}
View
2  src/main/java/com/clearspring/analytics/stream/cardinality/HyperLogLog.java
@@ -125,7 +125,7 @@ public HyperLogLog(int log2m, RegisterSet registerSet)
@Override
public boolean offer(Object o)
{
- final int x = MurmurHash.hash(o.toString().getBytes());
+ final int x = MurmurHash.hash(o);
// j becomes the binary address determined by the first b log2m of x
// j will be between 0 and 2^log2m
final int j = x >>> (Integer.SIZE - log2m);
View
10 src/main/java/com/clearspring/analytics/stream/cardinality/LinearCounting.java
@@ -16,11 +16,13 @@
package com.clearspring.analytics.stream.cardinality;
-import java.io.Serializable;
-import java.util.Arrays;
-import com.clearspring.analytics.util.IBuilder;
import com.clearspring.analytics.hash.MurmurHash;
+import com.clearspring.analytics.util.IBuilder;
+
+import java.io.Serializable;
+
+import java.util.Arrays;
/**
* See <i>A Linear-Time Probabilistic Counting Algorithm for Database Applications</i>
@@ -81,7 +83,7 @@ public boolean offer(Object o)
{
boolean modified = false;
- long hash = (long)MurmurHash.hash(o.toString().getBytes());
+ long hash = (long)MurmurHash.hash(o);
int bit = (int)((hash & 0xFFFFFFFFL) % (long)length);
int i = bit/8;
byte b = map[i];
View
2  src/main/java/com/clearspring/analytics/stream/cardinality/LogLog.java
@@ -120,7 +120,7 @@ public boolean offer(Object o)
{
boolean modified = false;
- int x = MurmurHash.hash(o.toString().getBytes());
+ int x = MurmurHash.hash(o);
int j = x >>> (Integer.SIZE - k);
byte r = (byte)(Integer.numberOfLeadingZeros( (x << k) | (1<<(k-1)) )+1);
if(M[j] < r)
View
13 src/test/java/com/clearspring/analytics/stream/cardinality/TestLinearCounting.java
@@ -16,16 +16,13 @@
package com.clearspring.analytics.stream.cardinality;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import com.clearspring.analytics.stream.cardinality.LinearCounting.Builder;
+import com.clearspring.analytics.stream.cardinality.LinearCounting.LinearCountingMergeException;
+import org.junit.Test;
import java.util.Arrays;
-import org.junit.Test;
-
-import com.clearspring.analytics.stream.cardinality.LinearCounting.Builder;
-import com.clearspring.analytics.stream.cardinality.LinearCounting.LinearCountingMergeException;
+import static org.junit.Assert.*;
public class TestLinearCounting
{
@@ -41,7 +38,7 @@ public void testComputeCount()
lc.offer(17);
lc.offer(18);
lc.offer(19);
- assertEquals(24, lc.computeCount());
+ assertEquals(27, lc.computeCount());
}
@Test

No commit comments for this range

Something went wrong with that request. Please try again.