In the for loop, the timing to print the progress is determined by ** bit operation "i & 0b1111 ... == 0" **. (Often judged by "i% 1000 == 0" etc.)
To adjust the display frequency **, just add "1" **. Since it is a 2-bit value, the frequency changes by 2 times. If you just need to adjust it appropriately (not the exact frequency), just add "1", so it's easy.
One bit "&" operation is OK. The processing load is lower than "%" (modulo operation).
trial.py
# -*- coding: utf-8 -*-
#for loop progress%Display timing "i& 0b1111.. ==Method using "0"
# note: np.arange(10)It seems that memory will be used abundantly if a large number of copies of
import numpy as np
# sec: main
def main(if_print=True):
if if_print:
print("started:", end="")
t = np.arange(10) #For processing load
n = 100000000 #Number of iterations
y = [] #For results
for i in range(n):
# sec:processing
y.append(t + i) #For processing load
# sec:Progress display
if if_print:
if i & 0b1_1111_1111_1111_1111_1111 == 0:
print(f"{i / n * 100:.1f}%", end="")
elif i & 0b1_1111_1111_1111_1111 == 0:
print(".", end="", flush=True)
if if_print:
print(" ok")
print("finished.")
del y
# sec: entry
if __name__ == "__main__": main()
Console output example:
started:0.0%...............2.1%...............4.2%..............
.6.3%...............8.4%...............10.5%...............12.6%
...............14.7%...............16.8%...............18.9%....
...........21.0%...............23.1%...............25.2%........
.......27.3%...............29.4%...............31.5%............
...33.6%...............35.7%...............37.7%...............3
9.8%...............41.9%...............44.0%...............46.1%
...............48.2%...............50.3%...............52.4%....
...........54.5%...............56.6%...............58.7%........
.......60.8%...............62.9%...............65.0%............
...67.1%...............69.2%...............71.3%...............7
3.4%...............75.5%...............77.6%...............79.7%
...............81.8%...............83.9%...............86.0%....
...........88.1%...............90.2%...............92.3%........
.......94.4%...............96.5%...............98.6%.......... ok
finished.
When the above-mentioned trial.py is executed, the memory is used more than necessary, and the memory usage rate of the OS exceeds 90% immediately. It seems that memory will be used abundantly when a large number of copies of np.arange (10) are added to the list. (PC will not become unstable) After this process is completed, the memory usage rate of the OS will change from 36% to 27%, and it can be seen that the minimum amount of memory required for the currently running OS / application is about 27% (maybe).