J

#### Jack MacArthur

##### Guest

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>