The following Java program takes on average between 0.50 secs and 0.55 secs to run:
public static void main(String[] args) {
long startTime = System.nano
While not directly related to the question's environment, just for the curiosity, I did the same test on .NET Core 2.1, x64, release mode.
Here is the interesting result, confirming similar phonomena (other way around) happening over the dark side of the force. Code:
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
Console.WriteLine("2 * (i * i)");
for (int a = 0; a < 10; a++)
{
int n = 0;
watch.Restart();
for (int i = 0; i < 1000000000; i++)
{
n += 2 * (i * i);
}
watch.Stop();
Console.WriteLine($"result:{n}, {watch.ElapsedMilliseconds} ms");
}
Console.WriteLine();
Console.WriteLine("2 * i * i");
for (int a = 0; a < 10; a++)
{
int n = 0;
watch.Restart();
for (int i = 0; i < 1000000000; i++)
{
n += 2 * i * i;
}
watch.Stop();
Console.WriteLine($"result:{n}, {watch.ElapsedMilliseconds}ms");
}
}
Result:
2 * (i * i)
2 * i * i