This looks like a very small problem, if we are in. NET, it is easy to use System.DateTime.Now directly to get to the current time, but also to perform a variety of different calculations or outputs. But such a problem, in rust, still took me some time. Of course, I think this kind of toss-up also has some benefits, from which we can better understand some of rust's internal principles.
First, let's see what we can do.
Rust default comes with the STD library, there is no time-based function, if we want to process time (to get the current time, or to calculate two time intervals, etc.), we need to introduce an additional library, name is
Http://doc.rust-lang.org/time/time/index.html
All we have to do is modify the Cargo.toml file.
Then, in the specific code file (RS) that needs to use this feature, import the library by using the following statement
extern Crate time; Crate is a very important concept, similar to the concept of package
Rust has a corresponding Crate.io site that can be used for rust developers around the world to search for or publish crate places
Then using the USE statement, complete the type import
Use time::*;
A few common methods are as follows
Time::now (), gets the current full time, including the date
Time::get_time (), gets the current time only
Here's an example, I'm trying to count the efficiency of a piece of code, get the current time before it executes, get the current time again after execution, and then the difference between the two is time consuming.
extern Crate time;use std::thread;use time::*; FN Main () {let start = Time::now (); Get start time let handles:vec<_> = (0..10). Map (|_|{ Thread::spawn (| | {Let mut x= 0; for inch (0..5_000_000) { x+=1 } ) }). Collect (); for in handles{ println! ( "Thread finished with count={}" "Could not join a thread!"). Unwrap ()); } Let end = Time::now (); //Get end time println! ("Done!start: {:?},end: {:?},duration:{:?}", Start,end,end-start);}
The effect of the last run is this
This program runs in debug mode for approximately 0.55 seconds (Nanos, refers to nanoseconds, 1 nanoseconds =0.000 000 001 seconds)
It takes about 0.001 seconds to run under release (equivalent to 1 milliseconds)
As can be seen here, the Rust program in debug mode and release mode performance is very different
One place to point out is the red part of the code below
println! ("Done!start: {:?},end:{:?}, duration:{:?}", Start,end,end-start);
Normally, when we print to the screen, we use a placeholder like {}, but when I used it before, I found I couldn't compile it. (In fact, it is linter-rust this plugin to help us check out the syntax errors, about linter-rust and plugin mechanism, interested can refer to http://www.cnblogs.com/chenxizhang/p/4759921.html)
The meaning of this sentence is that time:tm this struct (struct) does not implement FMT:: Display this method. We're going to http://doc.rust-lang.org/time/time/struct.Tm.html here to see that it's really not implemented display
But it implements the Debug method. This can actually be used to do the output. Only, the placeholder it requires is {:?}
There are two forms of implementation
It put me in a certain thought. In fact, this concept is similar to all types of ToString methods in C #. Only in C #, because all types are inherited from object, and the ToString method of object has a default implementation (the fully qualified name of the output type). Within a particular type, we will choose to override the ToString method to implement the custom output.
Here are two little questions, I'll take a look at it later, and take the time to write
1. How do I implement an extension method?
2. Can I inherit and override this method?
In addition, the idea of datetime, in fact in C #, is also a struct (struct), not a meta-type (Primitive type)
Rust Preliminary (iv): processing time in rust