OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

"Preprocessing" a python function, to avoid excess evaluation of conditional logic

  • Thread starter Thread starter Jack MacArthur
  • Start date Start date
J

Jack MacArthur

Guest
I am writing some python code that includes functions with two sets of parameters. The first set of parameters will be different every single time the function is called. The second set of parameters will be the same for large groups of function calls (e.g. these change every 10000s of evaluations of the function). I am hoping to write this function in such a way that the conditional logic is run only once whenever the values of the second set of parameters change. I have included some code that I think might work, but after some testing with the time module the improvement seems very small. Is this code actually doing what I am describing? And if not, is there some way to do this in python?

Code:
def preprocessor (parameter_array):
    
    # example parameters
    condition = parameter_array[0]
    index = parameter_array[1]

    def f (x_array):

        result = 0

        if (condition):
            result += x_array[index] ** 2
        else:
            result += x_array[index] - 5

        #.
        #.
        #.

        return result

    return f


# define some test parameters
test_parameters = [True, 1]

# run the function 100000 times with these parameters
function = preprocessor(test_parameters)

sum = 0

for i in range(100000):

    sum += function([i, i])

print(sum)

My hope is that, written in this way, the if statement is only evaluated whenever I call the preprocessor function. Thus, speeding up the evaluation of the for loop where only the x values change. This took around 0.024 seconds to run, while a control took around 0.028 seconds to run. I have also included the code I used for the control below (everything is without the time module for readability).

Code:
def f (x_array, parameter_array):
        
    # example parameters
    condition = parameter_array[0]
    index = parameter_array[1]

    result = 0

    if (condition):
        result += x_array[index] ** 2
    else:
        result += x_array[index] - 5

    #.
    #.
    #.

    return result


# define some test parameters
test_parameters = [True, 1]

# run the function 100000 times without using the preprocessed function

sum = 0

for i in range(100000):

    sum += f([i, i], test_parameters)

print(sum)

Thank you for your help,

Jack
<p>I am writing some python code that includes functions with two sets of parameters. The first set of parameters will be different every single time the function is called. The second set of parameters will be the same for large groups of function calls (e.g. these change every 10000s of evaluations of the function). I am hoping to write this function in such a way that the conditional logic is run only once whenever the values of the second set of parameters change. I have included some code that I think might work, but after some testing with the time module the improvement seems very small. Is this code actually doing what I am describing? And if not, is there some way to do this in python?</p>
<pre><code>def preprocessor (parameter_array):

# example parameters
condition = parameter_array[0]
index = parameter_array[1]

def f (x_array):

result = 0

if (condition):
result += x_array[index] ** 2
else:
result += x_array[index] - 5

#.
#.
#.

return result

return f


# define some test parameters
test_parameters = [True, 1]

# run the function 100000 times with these parameters
function = preprocessor(test_parameters)

sum = 0

for i in range(100000):

sum += function([i, i])

print(sum)
</code></pre>
<p>My hope is that, written in this way, the if statement is only evaluated whenever I call the <code>preprocessor</code> function. Thus, speeding up the evaluation of the for loop where only the x values change. This took around 0.024 seconds to run, while a control took around 0.028 seconds to run. I have also included the code I used for the control below (everything is without the time module for readability).</p>
<pre><code>def f (x_array, parameter_array):

# example parameters
condition = parameter_array[0]
index = parameter_array[1]

result = 0

if (condition):
result += x_array[index] ** 2
else:
result += x_array[index] - 5

#.
#.
#.

return result


# define some test parameters
test_parameters = [True, 1]

# run the function 100000 times without using the preprocessed function

sum = 0

for i in range(100000):

sum += f([i, i], test_parameters)

print(sum)
</code></pre>
<p>Thank you for your help,</p>
<p>Jack</p>
 

Latest posts

M
Replies
0
Views
1
MOHAMED AMIIN ABDI AADAN
M
Top