Skip to content

Instantly share code, notes, and snippets.

@benjchristensen
Created January 5, 2012 17:33
Show Gist options
  • Save benjchristensen/1566272 to your computer and use it in GitHub Desktop.
Save benjchristensen/1566272 to your computer and use it in GitHub Desktop.
Simple performance test of Math.random() for comparing machines
public class TestRandom {
public static void main(String args[]) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) {
Math.random();
}
System.out.println("Math.random() Time: " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
for (long i = 0; i < 8000000000L; i++) {
// perform random math functions to use CPU
int v = 4 * 67 + 87 / 45 * 2345;
}
System.out.println("Simple Math Time: " + (System.currentTimeMillis() - start) + "ms");
}
}
@benjchristensen
Copy link
Author

While benchmarking various EC2 instances I had code in my test app that used Math.random() to generate sample data.

I expected the cc2.8xlarge box to be faster than the cc1.4xlarge (twice the CPU cores, and chip architecture is newer by 2 years) but the cc1 was beating the cc2 in my tests.

After digging into it I found that by removing the Math.random() calls the cc2 began outperforming the cc1.

I wrote this small piece of code purely to isolate and exercise the Math.random() call, knowing that by itself it's a very poor benchmark, but it demonstrates the issue I found in the larger benchmark.

Here are the numbers showing how the cc2 outperforms the cc1 in a normal tight-loop doing math, but severely underperforms when doing Math.random():

Normal Math => cc2: 3137ms   cc1: 5600ms  (cc2 is faster)
Math.random => cc2: 3226ms   cc1: 2354ms  (cc2 is slower)

I don't know why this is, if it's the fault of the CPU, the Xen virtualization layer, Java or something else, but it definitely is a performance difference.

Between the two boxes though the only difference I can see is the underlying hardware as the OS and Java versions are the same (instances are launched from the same AMI).

EC2 Instance Type: cc2.8xlarge

Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz (from /proc/cpuinfo)

$ uname -a
Linux 2.6.18-164.15.1.el5 #1 SMP Wed Mar 17 11:30:06 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

$ env |grep TYPE
EC2_INSTANCE_TYPE=cc2.8xlarge

$ /usr/java/latest/bin/java -version
java version "1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)

$ /usr/java/latest/bin/java TestRandom
Math.random() Time: 3226ms
Simple Math Time: 3137ms

EC2 Instance Type: cc1.4xlarge

Intel(R) Xeon(R) CPU X5570 @ 2.93GHz (from /proc/cpuinfo)

$ uname -a
Linux 2.6.18-164.15.1.el5 #1 SMP Wed Mar 17 11:30:06 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

$ env |grep TYPE
EC2_INSTANCE_TYPE=cc1.4xlarge

$ /usr/java/latest/bin/java -version
java version "1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)

$ /usr/java/latest/bin/java TestRandom
Math.random() Time: 2354ms
Simple Math Time: 5600ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment