Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace time.time to time.monotonic for the purpose of measuring elapsed time #922

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

whitphx
Copy link
Contributor

@whitphx whitphx commented Aug 29, 2023

time.time() is not guaranteed to be monotonic.
So time.monotonic() should be used instead to measure elapsed time by subtracting two timestamps.
(There is a similar method, time.perf_counter(), but it's for perf-measuring on short-running code blocks.)

Ref: https://www.webucator.com/article/python-clocks-explained/

src/aiortc/contrib/media.py Outdated Show resolved Hide resolved
@jlaine
Copy link
Collaborator

jlaine commented Oct 25, 2023

This looks interesting but I'm puzzled by why you only made replacements in some parts of the code, it looks like there are other places where we use time.time() to measure time deltas?

@whitphx
Copy link
Contributor Author

whitphx commented Nov 19, 2023

@jlaine Thank you for the review.

I'm puzzled by why you only made replacements in some parts of the code, it looks like there are other places where we use time.time() to measure time deltas?

I simply missed other parts because I did grep-ing time.time in the src directory and made changes on the parts which I could judge could be changed at a glance with query words like "start" and "end".

@whitphx whitphx force-pushed the monotonic-time-for-elapsed-time branch from 1541a10 to 2c65163 Compare November 19, 2023 15:57
@whitphx
Copy link
Contributor Author

whitphx commented Nov 19, 2023

I fixed files in /examples as well.

Could you apply such changes on other parts because I couldn't judge if I could replace time.time with time.monotonic in other places by myself? If necessary, please ignore and close this PR as just a suggestion or a reference, and make another branch as your own.
Thanks.

@jlaine
Copy link
Collaborator

jlaine commented Nov 30, 2023

I belatedly realised we have an aiortc.clock.. I'm wondering whether we should consolidate time handling there?

Copy link

codecov bot commented Nov 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (9f14474) 100.00% compared to head (565f71e) 100.00%.
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #922   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           30        30           
  Lines         5816      5816           
=========================================
  Hits          5816      5816           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants