Everyone also saw the code ``` double hoge = 1;`` and said, "Is it okay if I don't set"
double hoge = 1.0d;` ``? "" Does the execution speed slow down? " I think you may have been worried.
So I actually measured it.
The unit is nanoseconds, but the speed varies depending on the environment, so it is just a guide. Think of it as ** relatively ** faster ** as the number is ** smaller. Bold is the conversion to the same type.
processing\result(dst)Type | int |
long |
float |
double |
---|---|---|---|---|
dst =int type variable; |
19.42 | 20.93 | 20.80 | 20.95 |
dst =long type variable; |
☓ | 20.92 | 20.84 | 20.94 |
dst =float type variable; |
☓ | ☓ | 21.10 | 20.84 |
dst =double type variable; |
☓ | ☓ | ☓ | 20.97 |
dst = (cast)int type variable; |
19.95 | 20.93 | 20.81 | 20.95 |
dst = (cast)long type variable; |
20.69 | 21.10 | 20.96 | 20.90 |
dst = (cast)float type variable; |
21.19 | 21.11 | 20.88 | 21.20 |
dst = (cast)double type variable; |
20.97 | 21.14 | 20.95 | 20.95 |
dst = 1; |
20.72 | 20.86 | 20.82 | 21.00 |
dst = 1l; |
☓ | 20.84 | 20.75 | 20.99 |
dst = 1.0f; |
☓ | ☓ | 20.72 | 21.07 |
dst = 1.0d; |
☓ | ☓ | ☓ | 20.94 |
After all it seems that it is basically faster to align the molds. However, there are some combinations where type conversion is faster, so it may be considered as a category of error.
double hoge = 1;And double hoge= 1.0d;As for, I found that it didn't change much, so I can finally sleep with my pillow raised from today.
### Caution
Based on this result ** Please do not try to speed up the program carelessly **.
Readability and algorithm speedup are more important than forcibly changing the program and making slight speedups such as errors.
# Execution content
The processing content actually entered is as follows.
static int srcI = 1;
static long srcL = 1l;
static float srcF = 1.0f;
static double srcD = 1.0d;
static int dstI;
static long dstL;
static float dstF;
static double dstD;
TimeMeasure.Sample sampleI[] = {
//Substitute as it is
()->{ dstI = srcI; }
// , ()->{ dstI = srcL; } //Impossible // , ()->{ dstI = srcF; } //Impossible // , ()->{ dstI = srcD; } //Impossible //Cast and assign , ()->{ dstI = (int)srcI; } , ()->{ dstI = (int)srcL; } , ()->{ dstI = (int)srcF; } , ()->{ dstI = (int)srcD; } //Substitute a number , ()->{ dstI = 1 ; } // , ()->{ dstI = 1l ; } //Impossible // , ()->{ dstI = 1.0f; } //Impossible // , ()->{ dstI = 1.0d; } //Impossible };
TimeMeasure.Sample sampleL[] = {
//Substitute as it is
()->{ dstL = srcI; }
, ()->{ dstL = srcL; }
// , ()->{ dstL = srcF; } //Impossible // , ()->{ dstL = srcD; } //Impossible //Cast and assign , ()->{ dstL = (long)srcI; } , ()->{ dstL = (long)srcL; } , ()->{ dstL = (long)srcF; } , ()->{ dstL = (long)srcD; } //Substitute a number , ()->{ dstL = 1 ; } , ()->{ dstL = 1l ; } // , ()->{ dstL = 1.0f; } //Impossible // , ()->{ dstL = 1.0d; } //Impossible };
TimeMeasure.Sample sampleF[] = {
//Substitute as it is
()->{ dstF = srcI; }
, ()->{ dstF = srcL; }
, ()->{ dstF = srcF; }
// , ()->{ dstF = srcD; } //Impossible //Cast and assign , ()->{ dstF = (float)srcI; } , ()->{ dstF = (float)srcL; } , ()->{ dstF = (float)srcF; } , ()->{ dstF = (float)srcD; } //Substitute a number , ()->{ dstF = 1 ; } , ()->{ dstF = 1l ; } , ()->{ dstF = 1.0f; } // , ()->{ dstF = 1.0d; } //Impossible };
TimeMeasure.Sample sampleD[] = {
//Substitute as it is
()->{ dstD = srcI; }
, ()->{ dstD = srcL; }
, ()->{ dstD = srcF; }
, ()->{ dstD = srcD; }
//Cast and assign
, ()->{ dstD = (double)srcI; }
, ()->{ dstD = (double)srcL; }
, ()->{ dstD = (double)srcF; }
, ()->{ dstD = (double)srcD; }
//Substitute a number
, ()->{ dstD = 1 ; }
, ()->{ dstD = 1l ; }
, ()->{ dstD = 1.0f; }
, ()->{ dstD = 1.0d; }
};
### Calculation method
1. Execute the process to be measured 1 million times and find the average execution time for each time.
2. Execute 1 100 times and calculate the average value of the results.
* The program for the calculation part is undecided because it is built into the homemade library. If you are distrustful of the result, try measuring it yourself.
<details>
<summary> Raw execution result </ summary>
<div>
Substitution to int sample( 1 / 6 ) set times : 1000000 x 100 set avelage : 19.416518 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 2 / 6 ) set times : 1000000 x 100 set avelage : 19.949821 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 3 / 6 ) set times : 1000000 x 100 set avelage : 20.694315 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 4 / 6 ) set times : 1000000 x 100 set avelage : 21.191030 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 5 / 6 ) set times : 1000000 x 100 set avelage : 20.969548 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 6 / 6 ) set times : 1000000 x 100 set avelage : 20.723962 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| Substitution to long sample( 1 / 8 ) set times : 1000000 x 100 set avelage : 20.926685 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 2 / 8 ) set times : 1000000 x 100 set avelage : 20.923104 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 3 / 8 ) set times : 1000000 x 100 set avelage : 20.933459 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 4 / 8 ) set times : 1000000 x 100 set avelage : 21.099284 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 5 / 8 ) set times : 1000000 x 100 set avelage : 21.107446 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 6 / 8 ) set times : 1000000 x 100 set avelage : 21.144695 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 7 / 8 ) set times : 1000000 x 100 set avelage : 20.855954 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 8 / 8 ) set times : 1000000 x 100 set avelage : 20.842132 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| Assignment to float sample( 1 / 10 ) set times : 1000000 x 100 set avelage : 20.799797 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 2 / 10 ) set times : 1000000 x 100 set avelage : 20.841888 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 3 / 10 ) set times : 1000000 x 100 set avelage : 21.097959 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 4 / 10 ) set times : 1000000 x 100 set avelage : 20.814193 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 5 / 10 ) set times : 1000000 x 100 set avelage : 20.959374 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 6 / 10 ) set times : 1000000 x 100 set avelage : 20.883601 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 7 / 10 ) set times : 1000000 x 100 set avelage : 20.948977 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 8 / 10 ) set times : 1000000 x 100 set avelage : 20.824086 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 9 / 10 ) set times : 1000000 x 100 set avelage : 20.750565 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 10 / 10 ) set times : 1000000 x 100 set avelage : 20.721974 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| Substitution to double sample( 1 / 12 ) set times : 1000000 x 100 set avelage : 20.958085 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 2 / 12 ) set times : 1000000 x 100 set avelage : 20.943837 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 3 / 12 ) set times : 1000000 x 100 set avelage : 20.839026 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 4 / 12 ) set times : 1000000 x 100 set avelage : 20.972230 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 5 / 12 ) set times : 1000000 x 100 set avelage : 20.951048 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 6 / 12 ) set times : 1000000 x 100 set avelage : 20.900803 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 7 / 12 ) set times : 1000000 x 100 set avelage : 21.196955 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 8 / 12 ) set times : 1000000 x 100 set avelage : 20.954024 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 9 / 12 ) set times : 1000000 x 100 set avelage : 21.002265 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 10 / 12 ) set times : 1000000 x 100 set avelage : 20.991709 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 11 / 12 ) set times : 1000000 x 100 set avelage : 21.072246 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=| sample( 12 / 12 ) set times : 1000000 x 100 set avelage : 20.941363 ns |=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|=|
</div></details>
### environment
Java : 1.8.0_31
OS : Windows 10 64bit
RAM : 32.0GB
CPU : Intel Core i7-4790K 4.00GHz
Recommended Posts