Last active
December 15, 2015 00:29
-
-
Save snodgrass23/5173452 to your computer and use it in GitHub Desktop.
speed test comparisons for Node and Ruby
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
function getRands(i) { | |
var r = []; | |
while (i--) { | |
r[i] = Math.floor(Math.random() * 5); | |
} | |
return r; | |
} | |
function findMatchScore(profileDimensions, verseDimensions) { | |
var i = profileDimensions.length; | |
var diff, total = 0; | |
while (i--) { | |
diff = (profileDimensions[i] - verseDimensions[i]); | |
total += diff * diff; | |
} | |
return total; | |
} | |
function randomMatch(profile) { | |
var verse = getRands(25); | |
var score = findMatchScore(profile, verse); | |
//console.log('score:', score); | |
} | |
function timeIt(i) { | |
var profile = getRands(25); | |
console.log('Matching profile:', profile); | |
while (i--) { | |
randomMatch(profile); | |
} | |
} | |
function testIt() { | |
var a = [0, 0, 0]; | |
var b = [0, 0, 0]; | |
var c = [4, 4, 4]; | |
console.log('should be 0:', findMatchScore(a, b)); | |
console.log('should be 48:', findMatchScore(a, c)); | |
} | |
timeIt(1000000); |
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
def rand_n(n, max) | |
randoms = [] | |
n.times do | |
randoms << rand(max) | |
end | |
randoms | |
end | |
def diff_score(a, b) | |
(a - b) * (a - b) | |
end | |
def match_score(a, b) | |
score = 0 | |
a.length.times do |i| | |
score += diff_score a[i], b[i] | |
end | |
return score | |
end | |
user = rand_n(25, 4) | |
1000000.times do | |
message = rand_n(25, 4) | |
match_score user, message | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just for fun, I wrote a go implementation: