1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python timeit timers - start and stop functionality?

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    Do Python timers have the ability to measure multiple intervals in the same timer object? I have a program that loops over thousands of objects and performs several actions e.g.: downloading, process, store, analyze). I'd like to be able to measure the cumulative time of each individual step when performed many times, but in a larger block of code that does other things.

    Is there any way to start and then stop (pause) timers, and then re-start them to measure some more time? Something like the following pseudocode?

    import timeit
    download_timer = <create a timer>
    processing_timer = <create a timer>

    for item_id in big_list_of_items:
    download_timer.start()
    slow_response = download_data(item_id)
    download_timer.pause()
    processing_timer.start()
    munge_data(slow_response)
    processing_timer.pause()

    log.debug('Cumulative download time: {} sec'.format(download_timer.elapsed))
    log.debug('Cumulative data processing time: {} sec'.format(processing_timer.elapsed))


    I am currently creating a new timer on each loop and then I have to keep track of the time in multiple variables that I add each increment to. Perusing the docs, it looks like that's the best option, short of creating my own class to do this, but I was hoping someone knows of a simpler way.

    Continue reading...

Compartilhe esta Página