Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Meow {
- static public final float PI = 3.1415927f;
- static public final float PI2 = PI * 2;
- static private final int SIN_BITS = 14; // 16KB. Adjust for accuracy.
- static private final int SIN_MASK = ~(-1 << SIN_BITS);
- static private final int SIN_COUNT = SIN_MASK + 1;
- static private final float radFull = PI * 2;
- static private final float degFull = 360;
- static private final float radToIndex = SIN_COUNT / radFull;
- static private final float degToIndex = SIN_COUNT / degFull;
- static public final float degreesToRadians = PI / 180;
- static private class Sin {
- static final float[] table = new float[SIN_COUNT];
- static {
- for (int i = 0; i < SIN_COUNT; i++)
- table[i] = (float)Math.sin((i + 0.5f) / SIN_COUNT * radFull);
- for (int i = 0; i < 360; i += 90) {
- table[(int)(i * degToIndex) & SIN_MASK] = (float)Math.sin(i * degreesToRadians);
- }
- }
- }
- static public void main (String[] args) throws Exception {
- float b = 1;
- while (b != 0) {
- System.gc();
- Thread.sleep(100);
- System.gc();
- Thread.sleep(50);
- float a;
- long s, e;
- s = System.nanoTime();
- for (int i = 0; i < 1250; i++)
- for (a = -MathUtils.PI2; a < MathUtils.PI2; a += 0.0001f)
- b += a;
- e = System.nanoTime();
- long loopTime = e - s;
- System.gc();
- Thread.sleep(100);
- System.gc();
- Thread.sleep(50);
- s = System.nanoTime();
- for (int i = 0; i < 1250; i++)
- for (a = -MathUtils.PI2; a < MathUtils.PI2; a += 0.0001f)
- b += sin(a);
- e = System.nanoTime();
- System.out.println("MathUtils.sin " + (e - s - loopTime) / 1e6);
- System.gc();
- Thread.sleep(100);
- System.gc();
- Thread.sleep(50);
- s = System.nanoTime();
- for (int i = 0; i < 1250; i++)
- for (a = -MathUtils.PI2; a < MathUtils.PI2; a += 0.0001f)
- b += sinQuadratic(a);
- e = System.nanoTime();
- System.out.println("sinQuadratic " + (e - s - loopTime) / 1e6);
- }
- }
- static public float sinQuadratic (float x) {
- if (x < -3.14159265)
- x += 6.28318531;
- else if (x > 3.14159265) x -= 6.28318531;
- if (x < 0) return 1.27323954f * x + 0.405284735f * x * x;
- return 1.27323954f * x - 0.405284735f * x * x;
- }
- static public float sin (float radians) {
- int i = (int)(radians * radToIndex) & SIN_MASK;
- return Sin.table[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment