Skip to content

Instantly share code, notes, and snippets.

@snodgrass23
Last active December 15, 2015 00:29
Show Gist options
  • Save snodgrass23/5173452 to your computer and use it in GitHub Desktop.
Save snodgrass23/5173452 to your computer and use it in GitHub Desktop.
speed test comparisons for Node and Ruby
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);
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
@jroes
Copy link

jroes commented Jul 10, 2013

Just for fun, I wrote a go implementation:

package main

import (
        "math/rand"
)

func main() {
  user := generateRand(25, 4)

  for i := 0; i < 1000000; i++ {
    numbers := generateRand(25, 4)
    matchScore(user, numbers)
  }
}

func generateRand(n int, max int) []int {
  numbers := make([]int, n)
  for i := 0; i < n; i++ {
    numbers[i] = rand.Intn(max)
  }
  return numbers
}

func matchScore(user []int, numbers []int) int {
  score := 0
  for i, _ := range user {
    score1, score2 := user[i], numbers[i]
    score += (score1 - score2) * (score1 - score2)
  }
  return score
}
○ → time go run revised.go

real    0m1.765s
user    0m1.723s
sys 0m0.040s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment