Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Speed up for loop
#1
Hello,
is there an alternative method to get the following code snippet faster?

var_refp.size contains 60001 elements, ground freq can change and is something between 200 and 400
That code is in an iteration loop and is executed about 10-50 times

 for k in range(0, var_refp.size, 1):
            if math.fmod(k, ground_frequenz) == 0:
                var_cur_ref = var_refp[k]
            var_refp[k] = var_cur_ref
Tank you Smile
Reply
#2
k is a value from range(), and is thus an int.  If ground_frequenz is also an int (as suggested by your post), then you could use the modulus operator instead of math.fmod, which could be faster.  ie:
for k in range(var_refp):
    if k % ground_frequenz == 0:
        # etc
But you should probably just profile it, to see if this is actually the slow part of what you're doing.
Reply
#3
As it appears from the @nilamo's post there is no need to set start=0 and step=1 for the range() function. These are the default values.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,324 Jan-21-2022, 06:24 PM
Last Post: mcva
  Python module speed or python speed in general Enrique6 1 1,853 May-04-2020, 06:21 PM
Last Post: micseydel
  Creating a program that records speed in a speed trap astonavfc 7 7,363 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