[PYTHON] Trial to judge the timing of the progress display of the for loop using 0b1111 ~

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.

image.png

Digression: Memory is used abundantly

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) image.png 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).

Recommended Posts

Trial to judge the timing of the progress display of the for loop using 0b1111 ~
How to display the progress bar (tqdm)
The third night of the loop with for
I want to display the progress bar
The second night of the loop with for
I want to display the progress in Python!
[Introduction to Python] How to stop the loop using break?
The story of using circleci to build manylinux wheels
Avoiding the pitfalls of using a Mac (for Linux users?)
Make the display of Python module exceptions easier to understand
Incorrect answer when using numpy.prod () for B problem of ABC169
The image display function of iTerm is convenient for image processing.
I tried to predict the up and down of the closing price of Gurunavi's stock price using TensorFlow (progress)
How to execute the sed command many times using the for statement
Vertically visualize the amount corresponding to the vertices of networkx using Axes3D
Real-time display of server-side processing progress in the browser (implementation of progress bar)
I tried to get the index of the list using the enumerate function
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Display the image of the camera connected to the personal computer on the GUI.
I wanted to challenge the classification of CIFAR-10 using Chainer's trainer
How to use machine learning for work? 01_ Understand the purpose of machine learning
I want to display the number of num_boost_rounds when early_stopping is applied using XGBoost callback (not achieved)
For the time being using FastAPI, I want to display how to use API like that on swagger
Use the progress bar with Click: (Easy to use, improve the display of tasks that take more than 24 hours, notes when using in parallel processing)