In the previous article, we compared the performance of C # and Rust by example, and it should be said that the calculation-intensive operations of rust in the release mode are some of the more obvious advantages. So, is it possible for us to do some quick application development in C #, and some of the core algorithms are implemented with rust? The answer is yes.
Writing Rust Code
The following code, saved in the lib.rs file
extern fn process () {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 ()); } println! ("done!");}
Several key points of this code are
1. Declared as pub, that is, to allow external access to the
2. The statement is extern, meaning should also say that you want external access
3. Add a tag #[no_mangle], which is said to prevent the compiler from renaming the function at compile time. I don't quite understand it, so let's do it.
The other part is the standard rust code.
Create a dynamic-link library of rust
By default, rust compiles libraries called static link libraries, and if we need to compile dynamic-link libraries, we need to define them in the Cargo.toml file.
Then, run the cargo build-release command to generate the dynamic-link library (DLL)
We can see a countlib.dll dynamic link library file in the output directory.
Use this dynamic link library in C #
You can put Countlib.dll under the root directory of the C # compiled output directory
usingSystem;usingSystem.Threading.Tasks;usingSystem.Diagnostics;usingSystem.Threading;usingSystem.Runtime.InteropServices;namespaceconsoleapplication1{classprogram {[DllImport ("Countlib.dll", callingconvention= callingconvention.cdecl)] public static extern void process (); Static voidMain (string[] args) {Stopwatch watch =NewStopwatch (); Watch. Start ();//parallel.for (0, ten, i = //{ //var x = 0; //for (int j = 0; j< 5000000; J + +) // { //X + = 1; // } //Console.WriteLine ("Thread: {0} complete count", Thread.CurrentThread.ManagedThreadId); //}); process (); //Call the program process inside rust to calculate Watch. Stop (); Console.WriteLine ("Time-consuming: {0} seconds", watch. Elapsed.totalseconds); Console.read (); } }}
The time spent in debug mode is 0.002 seconds (elevation is too obvious)
The time spent in release mode is 0.002 seconds (basically the same as debug mode, amazing)
The performance, then, is almost as close to the performance as the direct use of rust, which is 5 times times higher than the original C # approach.
In this sense, the task of computationally intensive (especially multi-threaded, multicore) can be written in rust and then called in C #.
"Special attention"
Cargo build is compiled by default according to the current computer's configuration, such as I am a 64-bit computer, then the compiled DLL is also 64 bits, in C # when used, it needs to be set to 64 bits, otherwise there will be an error
So, can the cargo build specify the corresponding platform to compile? Can be achieved by specifying the--target parameter, the available values are mainly
x86_64-pc-windows-gnu
i686-unknown-linux-gnu
x86_64-unknown-linux-gnu
详细可以参考 http://doc.crates.io/manifest.html
我用下面这样用就可以编译一个通用的dll(既能用于32位,也能用于64位——采用WOW模式)
In fact, this compilation option is similar to the one we used in Visual Studio to compile with any CPU.
Rust Preliminary (vi): Using rust components in C #