Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdded measure-elapsed-time example #343
Conversation
budziq
requested changes
Oct 25, 2017
| use std::time::{Duration, Instant}; | ||
| use std::thread; | ||
| fn expensive_function() { |
This comment has been minimized.
This comment has been minimized.
budziq
Oct 25, 2017
Collaborator
Lets hide the parts of the example that are not directly related to actual measurement with leading #
# use std:thread
#
# fn expensive_function() {
# thread::sleep(Duration::from_millis(10));
# }| let duration = Instant::now() - start(); | ||
| ``` | ||
|
|
||
| This is the fastest way of getting some quick and dirty |
This comment has been minimized.
This comment has been minimized.
budziq
Oct 25, 2017
Collaborator
I wouldn't exactly say that it's profiling nor benchmarking (there are better and easier ways to benchmark built into rust nightly or available in crates.io for stable), actually I would not like to even suggest that this is even a dirty form of either one of these. Rather just an elapsed time reporting that is also useful in some cases.
I'd suggest to remove or rewrite this last two paragraphs.
This comment has been minimized.
This comment has been minimized.
AlexEne
Oct 25, 2017
Author
Contributor
Ok, I missed the point a bit since the description was a bit generic. I will do a PR with these fixes later today.
| ```rust,no_run | ||
| let start = Instant::now(); | ||
| expensive_function(); | ||
| let duration = Instant::now() - start(); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
AlexEne
Oct 25, 2017
Author
Contributor
Ah, I see, I believed that it has to be a full rust program so it compiles (with main and all that)
| Another way to get the time [`Duration`] is as a difference of two | ||
| time [`Instant`] objects. | ||
|
|
||
| ```rust,no_run |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
|
||
| [![std-badge]][std] [![cat-time-badge]][cat-time] | ||
|
|
||
| One way to get the elapsed time is to directly use the method |
This comment has been minimized.
This comment has been minimized.
budziq
Oct 25, 2017
Collaborator
Let's reformat the description somewhat to a form like "Measures [time::Instant::elapsed] since [time::Instant::now]"
| expensive_function(); | ||
| let duration = start.elapsed(); | ||
| println!("Time elapsed in expensive_function() is: {:?}", duration); |
This comment has been minimized.
This comment has been minimized.
budziq
Oct 25, 2017
Collaborator
One possible tripwire here is that elapsed does not reset the start in any way.
We can suggest following or just mention the fact in the description.
let start = Instant::now();
expensive_function();
let (start, duration) = (Instant::now(), start.elapsed());
println!("Time elapsed in expensive_function() is: {:?}", duration);
some_other_function();
println!("Time elapsed in some_other_function() is: {:?}", start.elapsed());
This comment has been minimized.
This comment has been minimized.
AlexEne
Oct 25, 2017
Author
Contributor
I will just mention it in the description since I think this example will be a bit harder to follow and since start is reset before the println!(), makes it a bit imprecise.
budziq
merged commit 57ad48c
into
rust-lang-nursery:master
Oct 26, 2017
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I commented on that thread, I agree with the idea and thanks for all the feedback ;) |
This comment has been minimized.
This comment has been minimized.
|
Thanks! Not that much feedback was needed :) |
AlexEne commentedOct 24, 2017
•
edited by budziq
I just explored a bit this repo, read the contribution guidelines, one thing lead to another and I added the measure-elapsed-time example.
fixes #341