c:java_code>javac SpeedTest.java && java SpeedTest 5.776E+17, 1.075, 2.747, 0.391. c:java_code>javac SpeedTest.java && java SpeedTest 5.776E+17, 1.075, 2.742, 0.392. c:java_code>java SpeedTest 16 9.235E+18, 17.190, 2.930, 5.867. c:java_code>java SpeedTest 100 5.771E+19, 107.431, 9.990, 10.754. c:java_code>java SpeedTest 1 10 5.776E+19, 10.748, 27.741, 0.387. c:java_code>java SpeedTest 10 5.772E+18, 10.745, 2.846, 3.775. c:java_code>java SpeedTest 20 1.154E+19, 21.487, 3.711, 5.790. c:java_code>java SpeedTest 30 1.731E+19, 32.227, 4.559, 7.069. c:java_code>java SpeedTest 40 2.308E+19, 42.970, 5.500, 7.813. c:java_code>java SpeedTest 5 2.887E+18, 5.373, 2.828, 1.900. c:java_code>java SpeedTest 100 5.771E+19, 107.431, 8.992, 11.947. c:java_code>java SpeedTest 200 1.154E+20, 214.858, 16.149, 13.305. c:java_code>java SpeedTest 200 1.154E+20, 214.858, 15.068, 14.259. c:java_code>java SpeedTest 200 10 1.154E+22, 2148.585, 156.073, 13.767.MacBook Pro 2021
haowei@Haos-MacBook-Pro-2021-M1-Pro java_code % java SpeedTest 5.776E+17, 1.075, 4.482, 0.240. haowei@Haos-MacBook-Pro-2021-M1-Pro java_code % java SpeedTest 5.776E+17, 1.075, 4.509, 0.238. haowei@Haos-MacBook-Pro-2021-M1-Pro java_code % java SpeedTest 2 1.155E+18, 2.150, 4.583, 0.469. haowei@Haos-MacBook-Pro-2021-M1-Pro java_code % java SpeedTest 14 8.079E+18, 15.041, 7.040, 2.136. haowei@Haos-MacBook-Pro-2021-M1-Pro java_code % java SpeedTest 200 1.154E+20, 214.858, 83.269, 2.580. haowei@Haos-MacBook-Pro-2021-M1-Pro java_code % java SpeedTest 1 10 5.776E+19, 10.748, 44.697, 0.240.MacBook Pro 2018
多核跑分:
5.77e+21, 1074.31, 166.83, 6.44
单核跑分
5.78e+19, 10.75, 25.21, 0.47.5950X 跑分
执行结果:约为 15.50 左右
除了个别几次由于某些原因导致的降速度为10左右,大部分结果在15-16,故取15.50为性能结果。
for /L %i in (1,1,10) do @echo off && java SpeedTest # SUM, COUNT (B), TIME(s), SPEED(B/s) 5.77e+19, 107.43, 10.68, 10.06. 5.77e+19, 107.43, 10.33, 10.40. 5.77e+19, 107.43, 6.87, 15.64. 5.77e+19, 107.43, 11.94, 9.00. 5.77e+19, 107.43, 6.97, 15.41. 5.77e+19, 107.43, 6.46, 16.62. 5.77e+19, 107.43, 6.47, 16.61. 5.77e+19, 107.43, 7.05, 15.23. 5.77e+19, 107.43, 6.92, 15.52. 5.77e+19, 107.43, 6.77, 15.87.
CPU 占用情况
import java.util.Random;
public class SpeedTest {
public static Random rand = new Random(0);
public static void main(String[] args) throws Exception {
// show help
if(args.length > 0 && args[0].toLowerCase().equals("help"))
{
System.out.println("SUM, COUNT (B), TIME(s), SPEED(B/s)");
return;
}
// get arguments
int threadCount = args.length > 0 ? Integer.parseInt(args[0]) : 1;
int coefficient = args.length > 1 ? Integer.parseInt(args[1]) : 1;
// define variables
double[] results = new double[threadCount];
long[] counts = new long[threadCount];
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
threads[i] = new Thread("Subthread-" + i) {
@Override
public void run() {
int tid = Integer.parseInt(getName().split("-")[1]);
counts[tid] = 1048576L * (1024 + rand.nextInt(2)) * coefficient;
results[tid] = get_sum(0, counts[tid]);
}
double get_sum(long start, long end) {
double result = 0;
for(long i = start; i < end; i ++)
result += i;
return result;
}
};
}
// start all threads
var t1 = System.currentTimeMillis();
for (int i = 0; i < threads.length; i++)
threads[i].start();
// wait threads to finish
for (int i = 0; i < threads.length; i++)
threads[i].join();
var t2 = System.currentTimeMillis();
// sum all results
double total_sum = 0;
double total_count = 0;
for (int i = 0; i < threads.length; i++){
total_sum += results[i];
total_count += counts[i];
}
// format and output results
total_count /= 1E9; // in Billions
double time = (t2 - t1)/1000.0;
var speed = total_count / time;
System.out.printf("%8.3E, %8.3f, %8.3f, %8.3f.n", total_sum, total_count, time, speed);
}
}
测试代码 2.0
import java.util.ArrayList;
import java.util.Random;
public class SpeedTest {
public static double res = 0;
public static long count = 0;
public static Random rand;
public static double[] results;
public static long[] counts;
public static int[] elasptime;
public static int threadCount = 100;
public static void main(String[] args) throws Exception {
threadCount = 100;
Random rand = new Random(0);
results = new double[threadCount];
counts = new long[threadCount];
elasptime = new int[threadCount];
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
threads[i] = new Thread() {
@Override
public void run() {
var name = Thread.currentThread().getName();
if(!name.contains("-"))
return;
int tid = Integer.parseInt(name.split("-")[1]);
long len = 1024 * 1024 * (1024 + rand.nextInt(2));
double sum = 0;
for (long i = 0; i < len; i++)
sum += i;
counts[tid] = len;
results[tid] = sum;
}
};
}
// System.out.println("---- Start ----");
var t1 = System.currentTimeMillis();
for (int i = 0; i < threads.length; i++)
threads[i].start();
// System.out.println("---- Threads Join ---");
for (int i = 0; i < threads.length; i++)
threads[i].join();
var t2 = System.currentTimeMillis();
double total_sum = 0;
double total_count = 0;
for (int i = 0; i < threads.length; i++){
total_sum += results[i];
total_count += counts[i];
}
total_count /= 1E9; // in Billions
double time = (t2 - t1)/1000.0;
var speed = total_count / time;
System.out.printf("%5.2e, %5.2f, %5.2f, %5.2f.n", total_sum, total_count, time, speed);
// System.out.println("--- FINISH ---");
}
}
测试代码 1.0
import java.util.ArrayList;
import java.util.Random;
public class SpeedTest {
public static double res = 0;
public static long count = 0;
public static Random rand = new Random(0);
public static void main(String[] args) throws Exception {
// heavy_task();
double[] results = new double[100];
Thread[] threads = new Thread[30];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread() {
@Override
public void run() {
HwTimer timer = new HwTimer();
timer.start();
timer.result = 0;
timer.count = 1024 * 1024 * (1024 + rand.nextInt(2));
for (long i = 0; i < timer.count; i++)
timer.result += i;
timer.stop();
timer.show();
RandomDemo.count += timer.count;
// var res1 = heavy_task();
var name = Thread.currentThread().getName();
if(name.contains("-")) {
results[Integer.parseInt(name.split("-")[1])] = timer.result;
// System.out.println("timer.result: " + timer.result);
}
}
};
}
System.out.println("---- Start ----");
HwTimer timer = new HwTimer();
timer.start();
for (int i = 0; i < threads.length; i++)
threads[i].start();
// System.out.println("---- Threads Join ---");
for (int i = 0; i < threads.length; i++)
threads[i].join();
timer.stop();
timer.count = RandomDemo.count;
timer.result = 0;
for (int i = 0; i < threads.length; i++)
timer.result += results[i];
timer.show();
}
public static double heavy_task() {
HwTimer timer = new HwTimer();
timer.start();
timer.result = 0;
timer.count = 1024 * 1024 * (1021 + rand.nextInt(2));
for (long i = 0; i < timer.count; i++)
timer.result += i;
timer.stop();
timer.show();
return timer.result;
}
public static void randDemo() {
Random rand = new Random(11);
ArrayList origIDs = new ArrayList();
ArrayList randIDs = new ArrayList();
int count = 10;
for (int i = 0; i < count; i++)
origIDs.add(i);
while (origIDs.size() > 0) {
int pos = rand.nextInt(origIDs.size());
randIDs.add(origIDs.get(pos));
origIDs.remove(pos);
}
for (Integer i : randIDs)
System.out.print(i + ", ");
// output: 6, 8, 1, 3, 0, 5, 9, 4, 7, 2,
}
public static void speedDemo() {
var t1 = System.currentTimeMillis();
long result = 0;
long len = 1024 * 1024 * 1024;
len *= 3;
for (long i = 0; i < len; i++)
result += i;
var t2 = System.currentTimeMillis();
System.out.println("nresult: " + result + ", time: " + (t2 - t1) + "ms");
}
}
public class HwTimer {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public long startTime = 0;
public long endTime = 0;
public String name = "no name";
public double result = 1;
public long count = 1;
public void start() {
startTime = System.currentTimeMillis();
}
public void stop() {
endTime = System.currentTimeMillis();
}
public void show() {
var name = Thread.currentThread().getName();
var time = (endTime - startTime) / 1000.0;
var speed = count / time / 1E9;
var count1 = count * 1.0 / 1E9;
System.out.printf("%s: res=%5.2e, count=%5.2f B, time=%5.2fs, speed = %5.2f B/sn", name, result, count1, time, speed);
}
}



