-
-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Which usage is preferable? #53
Comments
Hello, I will be in favour of the 'G' implementation, so as to avoid an extra 'if' at each call. Notice that I will not put |
Thanks. The idea behind |
I quickly timeit-ed the two codes, with one situation in which there is no provided interval, and one where there is one:
Checking for So based on this, and if performance is an issue, I would go for |
Btw, given the values (in µs and even in ns), and given the amount of background processes I've on my computer when I executed this code, this "benchmark" should be considered with care :-) |
Good idea, to time it! I extended your tests to include checks outside the interval. I also replace the >>> import timeit
>>> F1, G1, F2, G2 = F(), G(), F(P.closed(0, 2)), G(P.closed(0, 2))
>>> sorted(timeit.repeat(lambda: F1(1), repeat=5, number=1000000))
[0.14253522500803228, 0.1521446229889989, 0.16290795200620778, 0.1756877709995024, 0.18972873299208004]
>>> sorted(timeit.repeat(lambda: G1(1), repeat=5, number=1000000))
[0.9611717640073039, 1.0052876260015182, 1.0283825780061306, 1.0961824119876837, 1.1008262019895483]
>>> sorted(timeit.repeat(lambda: F2(-1), repeat=5, number=1000000))
[0.6657288259884808, 0.6957280539936619, 0.7324803389929002, 0.7841811819962459, 0.8237168110063067]
>>> sorted(timeit.repeat(lambda: G2(-1), repeat=5, number=1000000))
[0.6685592129942961, 0.6988589969987515, 0.7048056549974717, 0.7982992489996832, 0.8394842549896566]
>>> sorted(timeit.repeat(lambda: F2(1), repeat=5, number=1000000))
[0.7109784969943576, 0.749733508011559, 0.75496754499909, 0.7824073730007512, 0.8159977729956154]
>>> sorted(timeit.repeat(lambda: G2(1), repeat=5, number=1000000))
[0.7004794439999387, 0.7452119939989643, 0.8100131639948813, 0.8110726650047582, 0.836370117001934]
>>> sorted(timeit.repeat(lambda: F2(3), repeat=5, number=1000000))
[0.6367974419990787, 0.6983797549910378, 0.7055405920109479, 0.7218278600048507, 0.7507733389938949]
>>> sorted(timeit.repeat(lambda: G2(3), repeat=5, number=1000000))
[0.6672344270045869, 0.7339863839879399, 0.7599738859862555, 0.7782973190041957, 0.786124837002717] Using the minimum execution times, I found that when actually checking the interval ( So, to boil it down:
Thanks for your help!! |
You're welcome :-) Another option would be to define two implementations of Speaking of an extra-lookup for a call, I'm also surprised by these benchmarks: loooking for
I guess that's because |
Switching between different |
Ok :-/ I looked at what happened with |
The gain is very limited, only around 10% and the code is, obviously, much less readable. |
What would be the preferred way to set an optional interval check?
I want to make a callable class, which checks, if the argument is in a given interval. The interval is an optional parameter, such as in these two classes
F
andG
:Which implementation (class
F
orG
) is preferable?Thanks for your help and insights!
The text was updated successfully, but these errors were encountered: