just compile, upload, and copy the program output . that's it!
Code: Select all
import lejos.hardware.Button;
import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.lcd.GraphicsLCD;
import lejos.hardware.lcd.LCD;
// hw brickbench
// benchmark test for NXT/EV3 and similar Micro Controllers
// PL: leJOS/EV3 0.9.x
// Autor: (C) Helmut Wunder 2013,2014
// Ported to leJOS by Andy Shaw.
// freie Verwendung für private Zwecke
// für kommerzielle Zwecke nur nach Genehmigung durch den Autor.
// protected under the friendly Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// http://creativecommons.org/licenses/by-nc-sa/3.0/
// version 1.09.2.ev3 release 2015-02
// PLEASE NOTE: This is not a good example of a Java program. It is a simple port of a C program.
// No attempt has been made to make use of Java features.
public class MBBenchmark
{
static long[] runtime = new long[8];
static int[] a = new int[500], b = new int[500], c = new int[500],
t = new int[500];
// --------------------------------------------
// Mersenne Twister
// --------------------------------------------
static int[] y = new int[25];
static int index = 25 + 1;
static final int M = 7;
static final int[] A = { 0, 0x8ebfd028 };
static int randM()
{
if (index >= 25)
{
int k;
if (index > 25)
{
int r = 9, s = 3402;
for (k = 0; k < 25; ++k)
{
r = 509845221 * r + 3;
s *= s + 1;
y[k] = s + (r >> 10);
}
}
for (k = 0; k < 25 - M; ++k)
y[k] = y[k + M] ^ (y[k] >> 1) ^ A[y[k] & 1];
for (; k < 25; ++k)
y[k] = y[k + (M - 25)] ^ (y[k] >> 1) ^ A[y[k] & 1];
index = 0;
}
int e = y[index++];
e ^= (e << 7) & 0x2b5b2500;
e ^= (e << 15) & 0xdb8b0000;
e ^= (e >> 16);
return e;
}
// --------------------------------------------
// Matrix Algebra
// --------------------------------------------
// matrix * matrix multiplication (matrix product)
static void MatrixMatrixMult(int N, int M, int K, double A[][],
double B[][], double C[][])
{
int i, j, s; // matrix A: N x M // B: M x K // C: N x K
for (i = 0; i < N; ++i)
{
for (j = 0; j < K; ++j)
{
C[i][j] = 0;
for (s = 0; s < M; ++s)
{
C[i][j] = C[i][j] + A[i][s] * B[s][j];
}
}
}
}
// matrix determinant
static double MatrixDet(int N, double A[][])
{
int i, j, i_count, j_count, count = 0;
double[][] Asub = new double[N - 1][N - 1];
double det = 0;
if (N == 1)
return A[0][0];
if (N == 2)
return (A[0][0] * A[1][1] - A[0][1] * A[1][0]);
for (count = 0; count < N; count++)
{
i_count = 0;
for (i = 1; i < N; i++)
{
j_count = 0;
for (j = 0; j < N; j++)
{
if (j == count)
continue;
Asub[i_count][j_count] = A[i][j];
j_count++;
}
i_count++;
}
det += Math.pow(-1, count) * A[0][count] * MatrixDet(N - 1, Asub);
}
return det;
}
// --------------------------------------------
// shell sort
// --------------------------------------------
static void shellsort(int size, int[] A)
{
int i, j, increment;
int temp;
increment = size / 2;
while (increment > 0)
{
for (i = increment; i < size; i++)
{
j = i;
temp = A[i];
while ((j >= increment) && (A[j - increment] > temp))
{
A[j] = A[j - increment];
j = j - increment;
}
A[j] = temp;
}
if (increment == 2)
increment = 1;
else
increment = (int) (increment / 2.2);
}
}
// --------------------------------------------
// benchmark test procedures
// --------------------------------------------
static int test_Int_Add()
{
int i = 1, j = 11, k = 112, l = 1111, m = 11111, n = -1, o = -11, p = -111, q = -1112, r = -11111;
int x;
int s = 0;
for (x = 0; x < 10000; ++x)
{
s += i;
s += j;
s += k;
s += l;
s += m;
s += n;
s += o;
s += p;
s += q;
s += r;
}
return s;
}
static int test_Int_Mult()
{
int x, y;
int s = 0;
for (y = 0; y < 2000; ++y)
{
s = 1;
for (x = 1; x <= 13; ++x)
{
s *= x;
}
for (x = 13; x > 0; --x)
{
s /= x;
}
}
return s;
}
static float test_float_math()
{
float s = (float) Math.PI;
int y;
for (y = 0; y < 1000; ++y)
{
s *= Math.sqrt(s);
s = (float) Math.sin(s);
s = (float) Math.exp(s);
s *= s;
}
return s;
}
static int test_rand_MT()
{
int s = 0;
int y;
for (y = 0; y < 5000; ++y)
{
s = randM() % 10001;
}
return s;
}
static float test_matrix_math()
{
int x;
double[][] A = new double[2][2], B = new double[2][2], C = new double[2][2];
double[][] O = new double[3][3], T = new double[3][3];
int s;
for (x = 0; x < 250; ++x)
{
A[0][0] = 1;
A[0][1] = 3;
A[1][0] = 2;
A[1][1] = 4;
B[0][0] = 10;
B[0][1] = 30;
B[1][0] = 20;
B[1][1] = 40;
MatrixMatrixMult(2, 2, 2, A, B, C);
A[0][0] = 1;
A[0][1] = 3;
A[1][0] = 2;
A[1][1] = 4;
MatrixDet(2, A);
O[0][0] = 1;
O[0][1] = 4;
O[0][2] = 7;
O[1][0] = 2;
O[1][1] = 5;
O[1][2] = 8;
O[2][0] = 3;
O[2][1] = 6;
O[2][2] = 9;
MatrixDet(3, O);
}
s = (int) (O[0][0] * O[1][1] * O[2][2]);
return s;
}
static int test_Sort()
{
int y;
int[] t = new int[500];
for (y = 0; y < 50; ++y)
{
System.arraycopy(a, 0, t, 0, t.length);
shellsort(500, t);
System.arraycopy(b, 0, t, 0, t.length);
shellsort(500, t);
System.arraycopy(c, 0, t, 0, t.length);
shellsort(500, t);
}
return y;
}
static long test_TextOut()
{
int y;
for (y = 0; y < 20; ++y)
{
LCD.clear();
LCD.drawString("" + 0 + " " + 1000 + " int_Add", 0, 0);
LCD.drawString("" + 0 + " " + 1010 + " int_Mult", 0, 1);
LCD.drawString("" + 0 + " " + 1020 + " float_op", 0, 2);
LCD.drawString("" + 0 + " " + 1030 + " randomize", 0, 3);
LCD.drawString("" + 0 + " " + 1040 + " matrix_algb", 0, 4);
LCD.drawString("" + 0 + " " + 1050 + " arr_sort", 0, 5);
LCD.drawString("" + 0 + " " + 1060 + " display_text", 0, 6);
LCD.drawString("" + 0 + " " + 1070 + " testing...", 0, 7);
}
return 99;
}
static int test_graphics()
{
int x = 1, y;
GraphicsLCD g = LocalEV3.get().getGraphicsLCD();
for (y = 0; y < 100; ++y)
{
g.clear();
g.drawArc(50, 40, 10, 10, 0, 360);
g.fillArc(30, 24, 10, 10, 0, 360);
g.drawLine(10, 10, 60, 60);
g.drawLine(50, 20, 90, 70);
g.drawRect(20, 20, 40, 40);
g.fillRect(65, 25, 20, 30);
g.drawArc(100, 30, 15, 20, 0, 360);
}
return x;
}
static void displayValue(int testNo, long testTime, String testName)
{
LCD.drawInt(testNo, 2, 0, testNo);
LCD.drawInt((int) testTime, 6, 3, testNo);
LCD.drawString(testName, 10, testNo);
}
static void displayValues()
{
displayValue(0, runtime[0], "int_Add");
displayValue(1, runtime[1], "int_Mult");
displayValue(2, runtime[2], "float_op");
displayValue(3, runtime[3], "randomize");
displayValue(4, runtime[4], "matrix_algb");
displayValue(5, runtime[5], "arr_sort");
displayValue(6, runtime[6], "displ_txt");
displayValue(7, runtime[7], "graphics");
}
static public void main(String[] args)
{
long time0;
int x, y;
float s;
int i;
LCD.clear();
LCD.drawString("hw brickbench", 0, 1);
LCD.drawString("(C)H.Wunder 2013", 0, 2);
LCD.drawString("initializing...", 0, 4);
// Java on the EV3 uses a JIT compiler, run the test multiple times to
// allow the results to be stable
for (int ii = 0; ii < 20; ii++)
{
for (y = 0; y < 500; ++y)
{
a[y] = randM() % 30000;
b[y] = randM() % 30000;
c[y] = randM() % 30000;
}
LCD.clear();
time0 = System.currentTimeMillis();
s = test_Int_Add();
runtime[0] = System.currentTimeMillis() - time0;
displayValue(0, runtime[0], "int_Add ");
System.out.println("int_Add " + runtime[0]);
time0 = System.currentTimeMillis();
s = test_Int_Mult();
runtime[1] = System.currentTimeMillis() - time0;
displayValue(1, runtime[1], "int_Mult");
System.out.println("int_Mult " + runtime[1]);
time0 = System.currentTimeMillis();
s = test_float_math();
runtime[2] = System.currentTimeMillis() - time0;
displayValue(2, runtime[2], "float_op");
System.out.println("float_op " + runtime[2]);
time0 = System.currentTimeMillis();
s = test_rand_MT();
runtime[3] = System.currentTimeMillis() - time0;
displayValue(3, runtime[3], "randomize");
System.out.println("randomize " + runtime[3]);
time0 = System.currentTimeMillis();
s = test_matrix_math();
runtime[4] = System.currentTimeMillis() - time0;
displayValue(4, runtime[4], "matrix_algb");
System.out.println("matrx_algb " + runtime[4]);
time0 = System.currentTimeMillis();
s = test_Sort();
runtime[5] = System.currentTimeMillis() - time0;
displayValue(5, runtime[5], "arr_sort");
System.out.println("arr_sort " + runtime[5]);
time0 = System.currentTimeMillis();
s = test_TextOut();
runtime[6] = System.currentTimeMillis() - time0;
System.out.println("text " + runtime[6]);
LCD.clear();
displayValues();
time0 = System.currentTimeMillis();
s = test_graphics();
runtime[7] = System.currentTimeMillis() - time0;
System.out.println("graphics " + runtime[7]);
LCD.clear();
displayValues();
}
Button.waitForAnyPress();
LCD.clear();
y = 0;
for (x = 0; x < 8; ++x)
{
y += runtime[x];
}
LCD.drawString("Total ms: " + y, 0, 0);
LCD.drawString("Benchmark: " + (50000000 / y), 0, 1);
System.out.println("total ms: " + y);
System.out.println("benchmark: " + 50000000 / y);
Button.waitForAnyPress();
LCD.clear();
}
}