When looking for where your code is slow, profile built-in module is handy. Here we can use it following way
import cProfile as profile
def func():
lists = [2.3, 4, 3,5.5, 6.5, 7.5, 6, 8]
newlist = []
a = 2
b = 7
for i in lists:
if min(a, b) < i < max(a, b):
newlist.append(i)
print(newlist)
profile.run('func()')
output is
[2.3, 4, 3, 5.5, 6.5, 6]
27 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <stdin>:1(func)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
8 0.000 0.000 0.000 0.000 {built-in method builtins.max}
8 0.000 0.000 0.000 0.000 {built-in method builtins.min}
1 0.000 0.000 0.000 0.000 {built-in method builtins.print}
6 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
As you might deduce max
and min
was used 8 times each but in this case once for min
and once for max
would be enough. Sample data is too tiny to say anything useful about time of execution of components. If you wish you might use more data (longer lists
) and look for results.