Performance penalty for using classes?
Miki Tebeka
tebeka at cs.bgu.ac.il
Wed Jan 15 02:50:58 EST 2003
Hello All,
Thanks for your help.
Looks like the winning combination will be struct + psyco + 2.3 which
<AMAZING> runs faster than the c test (compiled with -O3) </AMAZING>
[09:37] $time ./struct_test_psyco.py
Going 100000 iterations
* Done
real 0.25
user 0.25
sys 0.00
[09:37] $time ./c_t
c_test c_test.c
[09:37] $time ./c_test
Going 1000000 iterations
* Done
real 0.34
user 0.29
sys 0.04
Miki
--- c_test.c ---
#include <stdio.h>
#include <stdlib.h>
typedef struct {
long x;
long y;
} point_t;
point_t *
make_point(long x, long y)
{
point_t *pp;
pp = (point_t *)malloc(sizeof(point_t));
pp->x = x;
pp->y = y;
return pp;
}
long
point_x(point_t *pp)
{
return pp->x;
}
void
point_set_x(point_t *pp, long x)
{
pp->x = x;
}
long
point_y(point_t *pp)
{
return pp->y;
}
void
point_set_y(point_t *pp, long y)
{
pp->y = y;
}
void
test_c(long iterations)
{
point_t *pp;
long x, y;
while (iterations > 0) {
pp = make_point(random(), random());
x = point_x(pp);
y = point_y(pp);
point_set_x(pp, y);
point_set_y(pp, x);
--iterations;
}
}
int
main(int argc, char *argv[])
{
long iterations = 1000000;
if (argc > 1) {
iterations = atol(argv[1]);
}
printf("Going %ld iterations\n", iterations);
fflush(stdout);
test_c(iterations);
printf("* Done\n");
return 0;
}
--- c_test.c ---
--- struct_test_psyco.py ---
#!/usr/bin/env python
import random
def make_dot(x, y):
return [x, y]
def dot_x(dot):
return dot[0]
def dot_set_x(dot, x):
dot[0] = x
def dot_y(dot):
return dot[1]
def dot_set_y(dot, y):
dot[1] = y
def test_struct(iterations):
while iterations > 0:
d = make_dot(random.random(), random.random())
x, y = dot_x(d), dot_y(d)
dot_set_x(d, y)
dot_set_y(d, x)
iterations -= 1
if __name__ == '__main__':
from sys import argv, stdout
if len(argv) > 1:
iterations = long(argv[1])
else:
iterations = 100000
import psyco
for func in (make_dot, dot_x, dot_set_x, dot_y, dot_set_y, test_struct):
psyco.bind(func)
print 'Going %d iterations' % iterations
stdout.flush()
t = test_struct(iterations)
print '* Done'
--- struct_test_psyco.py ---
More information about the Python-list
mailing list