/* ------ Calcpi. CS -----*/
Using system;
Public class calcpi
{
Public const int COUNT = 100000000;
Public static void main (string [] ARGs)
{
Datetime start = datetime. now;
Random random = new random (start. millisecond );
Int inside = 0;
For (INT I = 0; I <count; I ++)
{
Double Cx = random. nextdouble ();
Double Cy = random. nextdouble ();
Double distance = math. SQRT (CX * CX) + (CY * CY ));
If (distance <1.0)
{
++ Inside;
}
}
Double Pi = 4 * (double) inside/(double) count;
Datetime end = datetime. now;
Timespan diff = end-start;
Console. writeline ("Pi = {0}", Pi );
Console. writeline ("consumed time: {0} ms", diff. totalmilliseconds );
}
}
Note:
1. This instance calculates the circumference RateProgramTo avoid accidental errors, the number of computations is greatly set.
Compilation process description
| Compile Parameters |
File Size |
File Name |
Execution time |
| /Optimize |
3,584 bytes |
Calcpi.exe |
8937.5 Ms |
| /Filealign: 512 |
3,584 bytes |
Calcpi512.exe |
8843.75 Ms |
| /Filealign: 1024 |
5,120 bytes |
Calcpi1024.exe |
9031.25 Ms |
| /Filealign: 8192 |
32,768 bytes |
Calcpi8192.exe |
8843.75 Ms |
You can see that using the/filealign parameter and bringing a small value (must be a multiple of 512) can reduce the size of the generated file moderately, without affecting execution efficiency. Another problem is that I used optimization but didn't improve the program efficiency. I don't know why.
The following are: