Skip to content

Commit

Permalink
adding randomized values test to prevent caching performance advantages
Browse files Browse the repository at this point in the history
  • Loading branch information
Malte Janduda committed Dec 25, 2012
1 parent 74dae61 commit df8fdaa
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cpp-test
cpp-test2
*.swp
*.swo
*~
7 changes: 7 additions & 0 deletions run2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -x

g++ test2.cpp -o cpp-test2

time pypy test2.py
time ./cpp-test2
66 changes: 66 additions & 0 deletions test2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
#include <cstdlib>

class Vector {
protected:
float a,b,c;

public:

Vector(float a, float b, float c) {
this->a = a;
this->b = b;
this->c = c;
}

float dot(Vector other) {
return this->a * other.a + this->b * other.b + this->c * other.c;
}

Vector cross(Vector other) {
return Vector(
this->b * other.c - this->c * other.b,
this->c * other.a - this->a * other.c,
this->a * other.b - this->b * other.a
);
}

void print() {
std::cout << "[ " << this->a << ", " << this->b << ", " << this->c << " ]" << std::endl;
}
};

int * randomList(unsigned int size) {
int * l = new int[size];

for (unsigned int i=0; i<size; i++)
l[i] = rand();

return l;
}

int main() {

unsigned int loops = 5e7;
unsigned int counter = 0;

std::cout << "looping " << loops << " times" << std::endl;

int * l = randomList(loops);

for(unsigned int i=0; i < loops; i++) {
Vector v1(1*l[i],2*l[i],3*l[i]);
Vector v2(9*l[i],8*l[i],7*l[i]);
v1.dot(v2);
v1.cross(v2);
counter++;
}

std::cout << "counter = " << counter << std::endl;

// v1.print();
// v2.print();
// std::cout << v1.dot(v2) << std::endl;
// v1.cross(v2).print();

}
52 changes: 52 additions & 0 deletions test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import random

class Vector(object):
def __init__(self):
self.a = 0.0
self.b = 0.0
self.c = 0.0

def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c

def __str__(self):
return "[ %s, %s, %s ]" % ( str(self.a), str(self.b), str(self.c) )

def dot(self, other):
return self.a * other.a + self.b * other.b + self.c * other.c

def cross(self, other):
v = Vector(
self.b * other.c - self.c * other.b,
self.c * other.a - self.a * other.c,
self.a * other.b - self.b * other.a
)
return v

def randomList(size):
return [random.randint(0,4294967295) for n in range(size)]

def main():

loops = int(5e7)
counter = 0

l = randomList(loops)

print "looping %s times" % loops

for i in range(loops):
v1 = Vector(1*(i % l[i]),2*(i % l[i]),3*(i % l[i]))
v2 = Vector(9*(i % l[i]),8*(i % l[i]),7*(i % l[i]))
v1.dot(v2)
v1.cross(v2)
counter = counter + 1

print "counter = %s" % counter
print v1
print v2
print v1.dot(v2)
print v1.cross(v2)
main()

0 comments on commit df8fdaa

Please sign in to comment.