Created
March 7, 2013 15:49
-
-
Save sscdotopen/5108988 to your computer and use it in GitHub Desktop.
This file contains 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
import org.apache.mahout.math.DenseVector; | |
import org.apache.mahout.math.Vector; | |
import org.apache.mahout.math.function.IntObjectProcedure; | |
import org.apache.mahout.math.map.OpenIntObjectHashMap; | |
import java.util.Random; | |
public class Benchmark { | |
static int NUM_FEATURES = 20; | |
static Random RANDOM = new Random(0xdeadbeef); | |
public static void main(String[] args) { | |
int numItems = 2500000; | |
OpenIntObjectHashMap<Vector> M = new OpenIntObjectHashMap<Vector>(numItems); | |
for (int itemID = 0; itemID < numItems; itemID++) { | |
M.put(itemID, randomVector(NUM_FEATURES)); | |
} | |
int numUsers = 20; | |
for (int n = 0; n < numUsers; n++) { | |
final Vector userFeatures = randomVector(NUM_FEATURES); | |
long start = System.currentTimeMillis(); | |
M.forEachPair(new IntObjectProcedure<Vector>() { | |
@Override | |
public boolean apply(int itemID, Vector itemFeatures) { | |
userFeatures.dot(itemFeatures); | |
return true; | |
} | |
}); | |
long pause = System.currentTimeMillis(); | |
M.forEachPair(new IntObjectProcedure<Vector>() { | |
@Override | |
public boolean apply(int itemID, Vector itemFeatures) { | |
dot(userFeatures, itemFeatures); | |
return true; | |
} | |
}); | |
long end = System.currentTimeMillis(); | |
System.out.println((pause - start) + " " + (end - pause)); | |
} | |
} | |
static double dot(Vector one, Vector two) { | |
double sum = 0; | |
for (int n = 0; n < NUM_FEATURES; n++) { | |
sum += one.getQuick(n) * two.getQuick(n); | |
} | |
return sum; | |
} | |
static Vector randomVector(int size) { | |
double[] values = new double[size]; | |
for (int n = 0; n < size; n++) { | |
values[n] = RANDOM.nextDouble(); | |
} | |
return new DenseVector(values, true); | |
} | |
} |
This file contains 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
Vector.dot() 832ms, direct dot 110ms | |
Vector.dot() 813ms, direct dot 96ms | |
Vector.dot() 650ms, direct dot 95ms | |
Vector.dot() 1584ms, direct dot 96ms | |
Vector.dot() 680ms, direct dot 105ms | |
Vector.dot() 728ms, direct dot 102ms | |
Vector.dot() 669ms, direct dot 102ms | |
Vector.dot() 669ms, direct dot 97ms | |
Vector.dot() 655ms, direct dot 98ms | |
Vector.dot() 668ms, direct dot 99ms | |
Vector.dot() 662ms, direct dot 116ms | |
Vector.dot() 671ms, direct dot 97ms | |
Vector.dot() 660ms, direct dot 96ms | |
Vector.dot() 649ms, direct dot 98ms | |
Vector.dot() 651ms, direct dot 96ms | |
Vector.dot() 670ms, direct dot 97ms | |
Vector.dot() 720ms, direct dot 98ms | |
Vector.dot() 653ms, direct dot 97ms | |
Vector.dot() 648ms, direct dot 97ms | |
Vector.dot() 662ms, direct dot 99ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment