Python: Difference between revisions

277 bytes added ,  27 December 2019
Line 179: Line 179:




===Misc===
===Timing Code===
[https://stackoverflow.com/questions/2866380/how-can-i-time-a-code-segment-for-testing-performance-with-pythons-timeit Reference]
<syntaxhighlight lang="python">
import time
 
t0 = time.time()
code_block
t1 = time.time()
 
total = t1-t0
</syntaxhighlight>
 
 
===if main===
If you are writing a script with functions you want to be included in other scripts, use <code>__name__</code> to detect if your script is being run or being imported.
If you are writing a script with functions you want to be included in other scripts, use <code>__name__</code> to detect if your script is being run or being imported.
[https://stackoverflow.com/questions/419163/what-does-if-name-main-do What does if __name__ == "__main__" do?]
[https://stackoverflow.com/questions/419163/what-does-if-name-main-do What does if __name__ == "__main__" do?]