Python Forum
How to speed up nested cycles?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to speed up nested cycles?
#1
I try to optimize and speed up the code. With numba, it started to work much better, but still takes hours because of a lot of data that should be iterated in nested cycles. How to be with these loops? How can these get optimized?

@njit
def function_gam(et0, y30, ph0b, ot, y70):
  ht = ot[0] * et0 * np.cos(y30) / np.sqrt(ph0b) + ot[1] * -et0 * np.sin(y30) / np.sqrt(ph0b) + y70 * np.sqrt(1 / ph0b) * et0 * np.sin(y30) + funct_labt(y30, ot, y70) * (np.sqrt(1 / ph0b) * (1 + et0 * np.cos(y30) * (1 + et0 * np.cos(y30)**2) / ph0b**2))
  return ht

@njit
def funct_labt(y30, ot, y70):
  y80 = ot[1] * et0 *(np.sin(y30)) / np.sqrt(ph0b) - y70 * (ph0b * et0 * (np.sin(y30)) / (1 + et0 * np.cos(y30)))**2 + (-ot[0] * et0 *(np.cos(y30)) / np.sqrt(ph0b))
  return y80

tol = E-10

for i in z30:# iteratation through values for variable z30
  for o in zres: # iteratation through values for array zres which includes 2 interconnected variales. The values of the first are in zres[0], for the other - in zres[1]
    for l in z70: # iteratation through values for variable z70
      ht0 = function_gam(et0, i, ph0b, o, l)
      if np.isclose(ht0, 0): # only these combinations of variables values are collected that satisfy the condition ht == 0
        lh0 = i, o[0], o[1], l
        print(lh0)
Reply
#2
What does this do:
tol = E-10
Is it supposed to affect this?
if np.isclose(ht0, 0)
Write your code in C or C++. That will speed thing up quite a bit. The kind of tools you are asking about are used when you have a Python program you need to speed up. You don't have a Python program. You have a program that is easily written in C or C++. I would say it is easier to write in C than Python. If needed you could spawn off the C/C++ program as a subprocess and plot results as they are generated. This would make things a lot faster and you could start using results before all results are generated.
Reply
#3
(Mar-24-2022, 08:31 PM)deanhystad Wrote: What does this do:
tol = E-10
Is it supposed to affect this?
if np.isclose(ht0, 0)
Write your code in C or C++. That will speed thing up quite a bit. The kind of tools you are asking about are used when you have a Python program you need to speed up. You don't have a Python program. You have a program that is easily written in C or C++. I would say it is easier to write in C than Python. If needed you could spawn off the C/C++ program as a subprocess and plot results as they are generated. This would make things a lot faster and you could start using results before all results are generated.

Yes, this

tol = E-10
Is for the following

if np.isclose(ht0, 0)
Reply
#4
That is not how it works. You need to specify tolerance as an argument in the isclose() function call. How is Python supposed to know that some variable you made up is supposed to be the tolerance used in some other function call? Function calls hardly ever use global variables, and the only way to pass information to most functions is as an argument.
if np.isclose(ht0, 0, atol = 1.0e-10):
Plus E-10 is not a number. You must have a variable "E" somewhere, otherwise this is a syntax error.
alexfrol86 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python module speed or python speed in general Enrique6 1 1,852 May-04-2020, 06:21 PM
Last Post: micseydel
  Creating a program that records speed in a speed trap astonavfc 7 7,361 Nov-07-2016, 06:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020