package br.com.newtc; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; public class Main extends ApplicationAdapter { SpriteBatch batch; OrthographicCamera camera; ShapeRenderer shapeRenderer; private Texture img; @Override public void create() { shapeRenderer = new ShapeRenderer(); batch = new SpriteBatch(); camera = new OrthographicCamera(); img = new Texture("badlogic.jpg"); runTests(); } @Override public void render () { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glEnable(GL20.GL_BLEND); Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); Gdx.gl.glDisable(GL20.GL_BLEND); /* batch.begin(); batch.draw(img, 0, 0); batch.end(); shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); shapeRenderer.circle(350, 350, 200); shapeRenderer.setColor(Color.RED); shapeRenderer.end();*/ int start = getTimeStamp(); shapeRenderer.begin(ShapeRenderer.ShapeType.Line); shapeRenderer.setColor(Color.BLACK); for (int i = 0; i < 8000; i++) { switch (rrandom(0) % 2) { case 0: shapeRenderer.rect(rrandom(0) % 150, rrandom(0) % 150, 10, 10); break; case 1: shapeRenderer.line(rrandom(0) % 150, rrandom(0) % 150, rrandom(0) % 150, rrandom(0) % 150); break; } } shapeRenderer.end(); int end = getTimeStamp(); String s = ("NM: " + (end - start) + " ms"); debug(s); } @Override public void dispose() { batch.dispose(); //img.dispose(); shapeRenderer.dispose(); } class TestClass { public int field1 = 70; public int field2 = 80; public int method1(int value) { return (field1 % value) + field2 / 2; } public int method2(int value) { return (field2 % value) + field1 / 2; } } ////////// random - by Sean Luke ///////////////// private static int lastSeed = 234123; /** bits should be <= 31. used by the random method */ private static int next(final int bits) { int IA = 16807; int IM = 2147483647; int IQ = 127773; int IR = 2836; int k = lastSeed / IQ; lastSeed = IA * (lastSeed - k * IQ) - IR * k; if (lastSeed < 0) lastSeed += IM; return lastSeed >> (31 - bits); } /** * This is a simple Linear Congruential Generator which produces random * numbers in the range [0,2^31), derived from ran0 in Numerical Recipies. * Note that ran0 isn't wonderfully random -- there are much better * generators out there -- but it'll do the job, and it's fast and has low * memory consumption. * * @param _seed * if > 0, sets the seed, if = 0, uses the last seed. You should * call Math.random(getTimeStamp()); * @return a positive 32 bits random int */ // guich@120: corrected so it uses the last rand if seed == 0. public static int rrandom(int _seed) { if (_seed != 0) { // strip out the minus-sign if any lastSeed = (_seed << 1) >>> 1; } int val = next(31); if (val < 0) val = -val; return val; } public int field1 = 70; public int field2 = 80; public void initUI() { runTests(); } public void runTests() { // Graphics g = getGraphics(); String s; int start, end, value; // // // paint tests // g.backColor = Color.WHITE; // g.fillRect(0, 0, 160, 160); // g.foreColor = Color.BLACK; // start = getTimeStamp(); // for (int i = 0; i < 8000; i++) // { // switch (rrandom(0) % 2) // { // case 0: // g.drawRect(rrandom(0) % 150, rrandom(0) % 150, 10, 10); // break; // case 1: // g.drawLine(rrandom(0) % 150, rrandom(0) % 150, rrandom(0) % 150, rrandom(0) % 150); // break; // } // } // end = getTimeStamp(); // updateScreen(); // add(new Label(s = ("native meth./graph: " + (end - start) + " ms")), LEFT, AFTER); // debug(s); // loop test start = getTimeStamp(); value = 100; for (int i = 0; i < 10000000; i++) { value = value / 7; if (value == 0) value = i / 3; } end = getTimeStamp(); s = ("Loop: " + (end - start) + " ms"); debug(s); // field test TestClass test = new TestClass(); start = getTimeStamp(); value = 100; for (int i = 0; i < 1000000; i++) { value += test.field1 + test.field2; test.field1 = i; test.field2 = i; value = value % 100; } end = getTimeStamp(); s = ("Field: " + (end - start) + " ms"); debug(s); // method test start = getTimeStamp(); value = 100; for (int i = 1; i < 1000000; i++) { value = test.method1(i); value += test.method2(i); value = value % 100; } end = getTimeStamp(); s = ("Method: " + (end - start) + " ms"); debug(s); debug(""+value); // array test byte array[] = new byte[30]; start = getTimeStamp(); for (int i = 0; i < 1000000; i++) for (int j = 0; j < 30; j++) array[j] = (byte) (array[j] + (j / 2)); end = getTimeStamp(); s = ("Array: " + (end - start) + " ms"); debug(s); // string test String strings[] = new String[3000]; for (int i = 0; i < strings.length; i++) strings[i] = "Num" + i; start = getTimeStamp(); for (int i = 0; i < 10000; i++) { String z = "All"; for (int j = 0; j < 30; j++) z += strings[j]; if (i == 10000) debug(z); } end = getTimeStamp(); s = ("String: " + (end - start) + " ms"); debug(s); } public int getTimeStamp() { return (int)System.currentTimeMillis(); } public void debug(String s) { System.err.println(s); } }