I develop a python program on my RPi (model B, rev 2 with the last version of Rapsbian). A part of my program takes some time to execute.
So, the program is:
Code: Select all
#!/usr/bin/python
import sys
from time import time
from math import pi
from math import pow
from math import sqrt
from math import fabs
# Luminosity threshold
LIGHT_THOLD = 100.0
# Luminosity generated by turning on light
LIGHT_GEN = 4.0*pi*100.0
# Distance between tow light in meter
LIGHT_DIST = 1.0
# Distance with the nearest light
LIGHT_NEAR = 1.0
# Luminosity without artificial light
LIGHT_BASE = 30.0
def distance(l_start, l_end):
dist_x = fabs(l_start-l_end)*LIGHT_DIST
dist_y = LIGHT_NEAR
return sqrt(pow(dist_x, 2.0) + pow(dist_y, 2.0))
def light_lum(lum_val):
return (LIGHT_THOLD-lum_val)*4.0*pi
########################################
# Main #
########################################
if len(sys.argv)<2:
print("Usage: "+sys.argv[0]+" <opt> <l0> <l1> <...>")
print("\topt:\t-l calcul luminosity with light values")
print("\t\t-r calcul light values with luminosity")
exit(1)
if sys.argv[1] == "-l":
light_val = sys.argv[2:]
#### Debut de l'algo ####
start = time()
for light in range(len(light_val)):
cpt = 0
lum = LIGHT_BASE
for rly in light_val:
lum += float(rly)/(4.0*pi*pow(distance(cpt, light), 2.0)) * LIGHT_GEN
cpt += 1
print("lum["+str(light)+"]: "+str(lum))
print ("exec time: "+str(time()-start))
#### Fin de l'algo ####
elif sys.argv[1] == "-r":
lum_val = float(sys.argv[2])
rly = light_lum(lum_val)*pow(distance(1, 1), 2.0) / LIGHT_GEN
print("rly for "+str(lum_val)+": "+str(rly))
else:
print("Usage: "+sys.argv[0]+" <opt> <l0> <l1> <...>")
print("\topt:\t-l calcul luminosity with light values")
print("\t\t-r calcul light values with luminosity")
exit(1)
exit(0)When I execute the program with the following arguments:
Code: Select all
python calc_lum.py -l 0.7 0.2 0.3 0.0 0.1 0.3 0.2 0.9 0.3 0.1 0.1 0.1 0.4 0.3 0.2 0.4For compare, I have executed this code with same arguments in my PC (AMD bi-core 1Ghz, 4Go RAM). The program takes less than 0.002 second. It's 20 time faster... it isn't normally if we just consider the CPU frequency.
I have remake the test with a C version of my program. The program takes 0.01 second on the RPi and 0.0003 second on my personal computer...
The python program on my computer is faster than the C program on the RPi 0_o
Do you know from where comes the problem? May be the cache management?
Thank's for your help!
Matthieu
