Hi Everyone I am Sachetan ,I was just experiementing with different operators to check wheather they are efficient or not , i found this when we use “==“ operator its takes more time compared to xnor+AND gate ,below is the picture as you can see ,why dont we just change the defination of “==” to use xnor+AND gate ? .Will their be any problem if we do that .
I’m actually not going to discuss the equality comparison here, but instead focus on the methodology: the way that your timing calculations are done.
Whenever you get timing results that are this short, they are basically meaningless. The number needs to be increased considerably before you can see any real result here. In general, unless your total time is at least half a second or so, add another zero to your iteration count and try again; once you have results over a second, you have something you can compare. On my computer, that requires at least two more orders of magnitude to get a reasonable result.
The second thing you need to be aware of, though, is that operations involving constants are often done once, as the program is being compiled, and aren’t done each time it’s run. Proving this with timeit.timeit() isn’t easy (though it is possible, and I did confirm before posting it here), but it’s a lot easier to demonstrate using a simple lambda function, and the dis module. Here’s what’s actually happening here:
In the first example, the comparison is done, and the result returned. But in the second, the XOR is actually precomputed; instead of calculating 5 ^ 5, Python just uses the number 0 as a constant. So instead of comparing, we’re just taking the boolean Not of zero. For a proper comparison, you can instead use a global variable that you compare to 5, which will prevent this optimization:
And now you can see that doing two operations (XOR followed by NOT) is slower than just one, which is more commonly what you will expect to see in Python.
But the most important point to note here is the immensely small diference between these two. In order to get over a second of runtime, I needed one billion iterations - which means that the numbers here are also the number of nanoseconds that each of these takes. Yes, this is the difference between taking six nanoseconds and nine nanoseconds. This is not meaningful to any program that is doing any sort of real work, so the correct takeaway from this isn’t that one of them is marginally faster, but that both of them are fast enough. Write the cleanest and most idiomatic code you can, and don’t worry about how fast it is - Python is pretty good at doing things efficiently without needing assembly-language tricks.
Please don’t do that. Post it as text. How are we supposed to run a picture?
That’s local, not global.
No, they take less. Because they’re repeated in a loop, and that loop costs a significant part of the time. For something this tiny, better repeat the statement, for example:
Hmm, I was under the impression that they were saved into the local function’s globals (in the “globals” sense of exec(code, locals, globals)). Are they all actually locals? That may mean it’s necessary, to defeat some types of optimization, to use setup="global x; x = 5" to force it to actually be a global (which can’t be optimized).
Well, yes. But my point was that this amount of time is negligible, so really only the scale of the time is relevant. These figures are in nanoseconds. NONE of this is a significant amount of time.