Write code to make CPU usage display a sine wave

后端 未结 5 595
无人及你
无人及你 2020-12-12 18:31

Write code in your favorite language and let Windows Task Manager represent a sine wave in CPU Usage History.

This is a technical in

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-12 19:05

    Here's a slightly modified @flodin's solution in Python:

    #!/usr/bin/env python
    import itertools, math, time, sys
    
    time_period = float(sys.argv[1]) if len(sys.argv) > 1 else 30   # seconds
    time_slice  = float(sys.argv[2]) if len(sys.argv) > 2 else 0.04 # seconds
    
    N = int(time_period / time_slice)
    for i in itertools.cycle(range(N)):
        busy_time = time_slice / 2 * (math.sin(2*math.pi*i/N) + 1)
        t = time.clock() + busy_time
        while t > time.clock():
            pass
        time.sleep(time_slice - busy_time);    
    

    A CPU-curve can be fine-tuned using time_period and time_slice parameters.

提交回复
热议问题