Python speed math test

i did a test with python math to check python speed

in 2.79 i get a time of 0.77 seconds for 6 E6 simple math operations

redid test under BL 3.6 and get

in Bl 3.6 i get a time of 0.97 seconds for 6 E6 simple math operations

is there some logic why python under 3.6 is slower then in 2.79 ?

thanks for any feedback
happy bl

Cant help without seeing the code

does not really matter same program in 2 Different Blender versions

``````
but here it is very simple python addon

import bpy, time
from time import time

ts = time()

print ()
print (' ^^^^    Begin loop  ^^^^^ ')
print ()
a1=2012e4
b1=2020e2

nloop = 6000000

for i in range(nloop):

C1 = a1+b1
#	print ('i=',i,' val=',C1)

print("n1=%3.1f  "%(nloop))
print  ( '{0:.5}'.format(1/3) )				# for float
print (' Count =','{:,}'.format(nloop))

print ('Loop: {Count}'.format(Count=nloop))

print ()
#print('Time in seconds =', time()-ts)
print("Time in seconds=%3.4f  "%(time()-ts))
print ()

``````

happy bl

there are too many differences to really narrow it down without a profiler- Blender itself is significantly different, and the two versions of blender are using two completely different versions of Python (separated by over a decade of development)

1 Like

Wellâ€¦ itâ€™s just not a very good way to benchmark thisâ€¦ letâ€™s look at this (i added something to acess a calling parameterâ€¦ you see laterâ€¦)

``````import sys
from time import time

ts = time()

a1=2012e4
b1=2020e2

print(sys.argv[1], end='  ')

nloop = 9876543

for i in range(nloop):
C1 = a1+b1

print("Time in seconds=%3.4f  "%(time()-ts))
``````

and now i call this on my linux with Python 3.5, 3.7 and 3.9 installed like so:

``````for p in 3.5 3.7 3.9; do for ((i=0;i<10;i++)); do echo \$(python\$p loop.py "Python\$") ; done; done > result.txt
``````

â€¦then i get (with adding to extra linefeeds) that:

``````Python3.5 Time in seconds=1.1560
Python3.5 Time in seconds=1.1885
Python3.5 Time in seconds=1.1486
Python3.5 Time in seconds=1.2084
Python3.5 Time in seconds=1.3092
Python3.5 Time in seconds=0.9581
Python3.5 Time in seconds=0.9003
Python3.5 Time in seconds=0.9977
Python3.5 Time in seconds=1.3423
Python3.5 Time in seconds=1.1289

Python3.7 Time in seconds=1.2766
Python3.7 Time in seconds=0.8587
Python3.7 Time in seconds=1.0876
Python3.7 Time in seconds=0.8426
Python3.7 Time in seconds=0.8258
Python3.7 Time in seconds=1.0890
Python3.7 Time in seconds=1.0189
Python3.7 Time in seconds=1.0318
Python3.7 Time in seconds=0.8617
Python3.7 Time in seconds=0.9006

Python3.9 Time in seconds=1.0996
Python3.9 Time in seconds=0.9145
Python3.9 Time in seconds=1.1568
Python3.9 Time in seconds=1.1919
Python3.9 Time in seconds=1.1773
Python3.9 Time in seconds=0.8924
Python3.9 Time in seconds=0.8614
Python3.9 Time in seconds=0.9990
Python3.9 Time in seconds=1.2007
Python3.9 Time in seconds=0.9502
``````

â€¦so whatever core of my 4 virtual / 2 real core processor gets the job assign toâ€¦ will does whatever it canâ€¦ and you see the difference isâ€¦

â€¦well i didnâ€™t add the code to actually compute the mean valueâ€¦ but i hope you get the ideaâ€¦

(also there are dozen of tutorials and tips how to use the best method to iterateâ€¦ because in python this depends heavily on the situation what you are actually doing like using the built in map function to process lists or using numpy array to make some mathâ€¦)

1 Like

looks like there are some differences for whatever reasons!

i was just looking at the 2 different bl version
for a very simple loop to see speed of my machine Hardware
and if any differences between versions

and will redo same test when i get a new machine later on

i did not use the quickest algo or method to do the loop as such

thanks for feedback
happy bl

i did another test using numpy vars
and i get time = 1.2 sec

not certain why numpy is not faster !

happy bl

Once again you would have to show the code for numpy. Numpy is quite heavy and is often slower unless you use it as intended. For example adding items to an array in numpy is much slower than adding them to a list in python. Or even doing loops on arrays. You need to use numpy functions to do the math. If you are using a python loop you are often using numpy wrong.

Numpy example. Yes I used gpt Iâ€™m on my phoneâ€¦ but it proves the point.

``````import numpy as np
import time

ts = time.time()

print()
print(' ^^^^    Begin loop  ^^^^^ ')
print()

a1 = 2012e4
b1 = 2020e2

nloop = 6000000

a1_array = np.full(nloop, a1)
b1_array = np.full(nloop, b1)

C1_array = a1_array + b1_array

print("n1=%3.1f  " % (nloop))
print('{0:.5}'.format(1 / 3))  # for float
print(' Count =', '{:,}'.format(nloop))

print('Loop: {Count}'.format(Count=nloop))

print()
print("Time in seconds=%3.4f  " % (time.time() - ts))
print()
``````

i made new script with your example
and still get T = 0.07 Sec

guess your right need to make it Numpy codes instead of python commands

thanks
happy bl