Diabolo
Posts: 12
Joined: Tue Aug 06, 2013 12:56 pm

Python execution performance

Fri Aug 16, 2013 12:44 pm

Hello,

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)
The program's relevant part is between lines ~50 and ~60.
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.4
The program takes 0.04 second, it's unbounded!

For 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

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26716
Joined: Sat Jul 30, 2011 7:41 pm

Re: Python execution performance

Fri Aug 16, 2013 12:47 pm

There's no way the C program should be that slow in comparison. Have you compiled with optimisation and release build turned on?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

Diabolo
Posts: 12
Joined: Tue Aug 06, 2013 12:56 pm

Re: Python execution performance

Sat Aug 17, 2013 10:21 am

I have compiled the C program in release mode and without optimisation.

Heater
Posts: 16092
Joined: Tue Jul 17, 2012 3:02 pm

Re: Python execution performance

Sat Aug 17, 2013 4:22 pm

What is your question exactly? What relative speeds to expect?

Off the top of my head and without actually measuring anything I would expect something like:

1) The Pi is about 3 times slower raw clock speed than my PC.
2) The ARM processor is probably 2 to 4 times slower per clock as I suspect it does not have such big amounts of instruction pipe lining and other internal parallelism as my Intel PC processor.
3) The ARM no doubt has much small caches than my Intel, which will slow thing considerably depending on the size of the "working set" of my algorithm. Lets throw in 2 to 4 times slowdown for that.

Total slow down is then 3 * 2 * = 12 times.
Or on the maximum side: 3 * 4 * 4 = 48 times.

So there we have it a slow down between 12 and 48 compared to my PC.

By the way, when doing such timing measurements it's better to arrange to do your algorithm enough times in a loop that the total execution time is a few seconds.
Memory in C++ is a leaky abstraction .

Diabolo
Posts: 12
Joined: Tue Aug 06, 2013 12:56 pm

Re: Python execution performance

Wed Aug 21, 2013 7:47 am

The ARM architecture is more simple than an Intel or AMD arch...
this explains why the difference between my personal computer and the RPi is so great.

Thank you!
Matthieu

Return to “Python”