Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Math.abs() discussion  (Read 7675 times)
0 Members and 1 Guest are viewing this topic.
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Posted 2006-02-20 13:19:05 »

I wanted to open up this discussion to those interested.  I was running some tests of my own and found there was no difference between Math.abs() and ((a ^ (a >> 31)) - (a >> 31)).  I just want to make sure that I am conducting these tests correctly.

First my machine specs:
HP Compaq P4 2.66GHz
768MB

EDIT: JDK = 1.5.0_05-b05

Here is the class that I set up:
1  
2  
3  
4  
5  
public final class MathTest {
  public static int abs(int a) {
    return ((a ^ (a >> 31)) - (a >> 31));
  }
}

When I run with this test code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class Test {
    public static void main(String args[]) {
        int a = -2;
        int b;
        long time1;
        long time2;

        time1 = System.nanoTime();
            b = MathTest.abs(a);
        time2 = System.nanoTime();

        System.out.println((time2 - time1));
    }
}

The times are:
MathTest.abs() = 1084774 nano seconds
Math.abs() = 41346 nano seconds

but when I change the test code to this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class Test {
    public static void main(String args[]) {
        int a = -2;
        int b;
        long time1;
        long time2;

        for(int i=0;i<1000000;i++) {
            b = Math.abs(a);
        }

        time1 = System.nanoTime();
        for(int i=0;i<100000;i++) {
            b = Math.abs(a);
        }
        time2 = System.nanoTime();

        System.out.println((((double)time2 - (double)time1) / 100000.0));
    }
}

The times are:
MathTest.abs() = 2.34 nano seconds
Math.abs() = 2.34 nano seconds


So when I do a warm up loop and then average 100,000 calls, there is no difference.  Am I running this test correctly?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2006-02-20 13:29:47 »

The VM is smart... very smart, so try to use 'b' later on, or it will be optimized away. (at least the server VM is that smart)

Further, dividing by "100000.0" makes little sense, it should be 1,000,000.0, and then change "nano" into "milli".

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Spasi
« Reply #2 - Posted 2006-02-20 13:34:07 »

Also, don't use the same value 100.000 times. Try using a randomly generated array of values.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #3 - Posted 2006-02-20 14:07:40 »

Here's a slightly cleaned up version of the test I used:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
import java.util.*;

public class Foo
{
    public static int abs(int a)
    {
        return (a^(a>>31))-(a>>31);
    }
   
    public static void main(String[] args)
    {
        Random random = new Random();
        int[] vals = new int[1000000];

        for (int round = 0; round < 100; round++)
        {
            for (int i = 0; i < vals.length; i++)
            {
                vals[i] = random.nextInt();
            }
         
            for (int testNum = 0; testNum < 2; testNum++)
            {
                int res = 0;
                long pre = System.nanoTime();
                switch (testNum)
                {
                    case 0: // Math.abs
                       for (int i=0; i<vals.length; i++)
                        {
                            res += Math.abs(vals[i]);
                        }
                        break;
                    case 1: // New abs
                       for (int i=0; i<vals.length; i++)
                        {
                            res += Foo.abs(vals[i]);
                        }
                        break;
                    }
                long post = System.nanoTime();
                System.out.println(testNum + ": " + (post - pre)/(float)vals.length + " ns (combined result: "+res+")");
            }
        }
    }
}

Play Minecraft!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2006-02-20 14:22:36 »

Working on an int[] of 1000000 elements (4MB) will skew the results because of cpu-cache trashing.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #5 - Posted 2006-02-20 14:25:39 »

Be that as it may, both functions suffer the same cpu-cache trashing.

And even with horrible cpu-cache trashing "skewing the results", and with a relatively slow for loop, the new abs method is three times faster.

Play Minecraft!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #6 - Posted 2006-02-20 14:27:18 »

Thanks.  I copied Markus's code and had similar results.  While the time taken was longer, but the speed was similar for both methods.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
0: 18.60879 ns (combined result: -1315403454)
1: 19.868448 ns (combined result: -1315403454)
0: 18.246174 ns (combined result: -1865390775)
1: 18.293943 ns (combined result: -1865390775)
0: 18.0766 ns (combined result: 1080804409)
1: 18.343952 ns (combined result: 1080804409)
0: 18.157057 ns (combined result: 1758684426)
1: 18.083584 ns (combined result: 1758684426)
0: 18.365463 ns (combined result: -1076665754)
1: 18.197006 ns (combined result: -1076665754)
0: 18.03665 ns (combined result: -97081660)
1: 18.5423 ns (combined result: -97081660)
0: 18.180801 ns (combined result: -2120513054)
1: 18.668573 ns (combined result: -2120513054)
0: 17.952282 ns (combined result: 290568654)
1: 18.221869 ns (combined result: 290568654)
0: 17.981615 ns (combined result: 177362767)
1: 18.23919 ns (combined result: 177362767)
0: 18.165436 ns (combined result: -2077948072)
1: 18.30512 ns (combined result: -2077948072)
0: 18.764397 ns (combined result: -1748804390)
1: 18.363228 ns (combined result: -1748804390)
0: 18.34088 ns (combined result: 903271049)
1: 18.352053 ns (combined result: 903271049)
0: 18.325233 ns (combined result: 1483533930)
1: 18.052015 ns (combined result: 1483533930)
0: 18.109007 ns (combined result: 552949311)
1: 18.071293 ns (combined result: 552949311)
0: 17.983011 ns (combined result: 1747946045)
1: 18.616331 ns (combined result: 1747946045)
0: 17.972956 ns (combined result: 1956568510)
1: 18.451508 ns (combined result: 1956568510)
0: 17.87797 ns (combined result: 591958754)
1: 18.182756 ns (combined result: 591958754)
0: 18.145325 ns (combined result: 1098472751)
1: 18.1199 ns (combined result: 1098472751)
0: 18.083023 ns (combined result: 534516993)
1: 18.115152 ns (combined result: 534516993)
0: 18.054249 ns (combined result: -1745121369)
1: 18.065985 ns (combined result: -1745121369)
0: 18.03162 ns (combined result: -1843254525)
1: 18.634212 ns (combined result: -1843254525)
0: 17.956472 ns (combined result: -1270942001)
1: 18.389488 ns (combined result: -1270942001)
0: 17.879646 ns (combined result: 334485842)
1: 18.222708 ns (combined result: 334485842)
0: 18.13806 ns (combined result: 1077322536)
1: 18.3934 ns (combined result: 1077322536)
0: 18.077717 ns (combined result: 1432730059)
1: 18.250084 ns (combined result: 1432730059)
0: 18.11124 ns (combined result: 1705086187)
1: 18.082745 ns (combined result: 1705086187)
0: 18.276344 ns (combined result: 197915591)
1: 18.236116 ns (combined result: 197915591)
0: 17.951723 ns (combined result: -262464572)
1: 18.143648 ns (combined result: -262464572)
0: 17.982174 ns (combined result: -2080455328)
1: 18.117107 ns (combined result: -2080455328)
0: 18.088612 ns (combined result: -1106795676)
1: 18.082745 ns (combined result: -1106795676)
0: 18.6574 ns (combined result: 760014021)
1: 18.350376 ns (combined result: 760014021)
0: 18.914974 ns (combined result: -1724064827)
1: 18.925867 ns (combined result: -1724064827)
0: 18.038326 ns (combined result: 1044698636)
1: 18.59538 ns (combined result: 1044698636)
0: 18.097273 ns (combined result: -595048485)
1: 18.257908 ns (combined result: -595048485)
0: 18.121857 ns (combined result: 1677961454)
1: 18.162924 ns (combined result: 1677961454)
0: 18.448156 ns (combined result: -1623322489)
1: 18.417145 ns (combined result: -1623322489)
0: 18.19952 ns (combined result: 263470699)
1: 18.126604 ns (combined result: 263470699)
0: 17.972675 ns (combined result: -1060058100)
1: 18.153984 ns (combined result: -1060058100)
0: 18.000051 ns (combined result: 1205721433)
1: 18.12074 ns (combined result: 1205721433)
0: 18.116268 ns (combined result: 818869219)
1: 18.562136 ns (combined result: 818869219)
0: 17.962337 ns (combined result: 1878388999)
1: 18.430555 ns (combined result: 1878388999)
0: 18.309032 ns (combined result: -2113436117)
1: 18.55124 ns (combined result: -2113436117)
0: 18.074924 ns (combined result: 1820666565)
1: 18.240866 ns (combined result: 1820666565)
0: 18.386135 ns (combined result: 1229473012)
1: 18.161247 ns (combined result: 1229473012)
0: 18.02408 ns (combined result: 1958246077)
1: 18.202871 ns (combined result: 1958246077)
0: 17.903671 ns (combined result: 2067500862)
1: 18.388927 ns (combined result: 2067500862)
0: 18.18639 ns (combined result: 1324947411)
1: 18.226896 ns (combined result: 1324947411)
0: 17.956192 ns (combined result: -1924925087)
1: 18.101463 ns (combined result: -1924925087)
0: 17.86568 ns (combined result: -532989700)
1: 18.20846 ns (combined result: -532989700)
0: 18.183596 ns (combined result: 513265447)
1: 18.16488 ns (combined result: 513265447)
0: 18.100904 ns (combined result: 2060306131)
1: 18.351772 ns (combined result: 2060306131)
0: 18.158731 ns (combined result: 2071619586)
1: 18.098667 ns (combined result: 2071619586)
0: 18.169067 ns (combined result: -471254426)
1: 18.295065 ns (combined result: -471254426)
0: 18.249529 ns (combined result: -611431217)
1: 18.32244 ns (combined result: -611431217)
0: 18.23444 ns (combined result: -299034051)
1: 18.184992 ns (combined result: -299034051)
0: 18.132193 ns (combined result: 1430033366)
1: 18.31322 ns (combined result: 1430033366)
0: 18.205385 ns (combined result: -887723694)
1: 18.821108 ns (combined result: -887723694)
0: 18.36211 ns (combined result: 737382911)
1: 18.259584 ns (combined result: 737382911)
0: 18.096155 ns (combined result: 380839450)
1: 18.126884 ns (combined result: 380839450)
0: 18.024635 ns (combined result: -1108004076)
1: 18.1903 ns (combined result: -1108004076)
0: 18.10286 ns (combined result: 1716848877)
1: 18.714949 ns (combined result: 1716848877)
0: 18.047266 ns (combined result: 473761060)
1: 18.38446 ns (combined result: 473761060)
0: 18.183876 ns (combined result: -985866728)
1: 17.968485 ns (combined result: -985866728)
0: 18.158451 ns (combined result: 1244643750)
1: 18.419937 ns (combined result: 1244643750)
0: 18.235277 ns (combined result: -993873792)
1: 18.250645 ns (combined result: -993873792)
0: 18.329704 ns (combined result: -641453691)
1: 18.160408 ns (combined result: -641453691)
0: 18.049223 ns (combined result: 1027949056)
1: 18.528893 ns (combined result: 1027949056)
0: 18.077436 ns (combined result: 228195184)
1: 18.170465 ns (combined result: 228195184)
0: 18.183315 ns (combined result: -233048849)
1: 18.193094 ns (combined result: -233048849)
0: 17.937475 ns (combined result: 1751515530)
1: 18.11096 ns (combined result: 1751515530)
0: 17.995024 ns (combined result: -1862998016)
1: 18.170189 ns (combined result: -1862998016)
0: 17.97016 ns (combined result: -968791412)
1: 18.506542 ns (combined result: -968791412)
0: 18.184155 ns (combined result: -1635237483)
1: 18.197285 ns (combined result: -1635237483)
0: 17.892498 ns (combined result: -1406768969)
1: 18.1646 ns (combined result: -1406768969)
0: 18.403456 ns (combined result: 1802608728)
1: 18.31825 ns (combined result: 1802608728)
0: 18.21265 ns (combined result: -1428482136)
1: 18.224941 ns (combined result: -1428482136)
0: 18.167393 ns (combined result: 1189860402)
1: 18.43642 ns (combined result: 1189860402)
0: 18.2783 ns (combined result: 701333708)
1: 18.476929 ns (combined result: 701333708)
0: 17.976028 ns (combined result: 1069482517)
1: 18.224384 ns (combined result: 1069482517)
0: 18.000612 ns (combined result: 530740760)
1: 18.286123 ns (combined result: 530740760)
0: 17.965693 ns (combined result: -1496340562)
1: 18.140297 ns (combined result: -1496340562)
0: 18.563251 ns (combined result: -1123274341)
1: 18.34088 ns (combined result: -1123274341)
0: 18.386135 ns (combined result: 1518703820)
1: 18.051456 ns (combined result: 1518703820)
0: 18.020725 ns (combined result: 715089612)
1: 18.232763 ns (combined result: 715089612)
0: 18.003407 ns (combined result: -973747273)
1: 18.19114 ns (combined result: -973747273)
0: 18.104536 ns (combined result: 1886468757)
1: 18.67444 ns (combined result: 1886468757)
0: 18.227177 ns (combined result: 1201661615)
1: 18.298695 ns (combined result: 1201661615)
0: 18.484192 ns (combined result: 848677264)
1: 18.046148 ns (combined result: 848677264)
0: 18.03246 ns (combined result: -1128007728)
1: 18.159012 ns (combined result: -1128007728)
0: 18.033298 ns (combined result: 688562529)
1: 18.19114 ns (combined result: 688562529)
0: 17.955912 ns (combined result: 500414561)
1: 18.262936 ns (combined result: 500414561)
0: 17.954515 ns (combined result: -778665564)
1: 18.198402 ns (combined result: -778665564)
0: 18.204548 ns (combined result: 400803130)
1: 18.136662 ns (combined result: 400803130)
0: 18.255114 ns (combined result: -1668956835)
1: 18.15594 ns (combined result: -1668956835)
0: 17.896969 ns (combined result: -40335860)
1: 18.136105 ns (combined result: -40335860)
0: 17.975748 ns (combined result: -654445336)
1: 18.178848 ns (combined result: -654445336)
0: 18.088053 ns (combined result: 2068805083)
1: 18.544256 ns (combined result: 2068805083)
0: 18.065985 ns (combined result: 1850106056)
1: 18.64762 ns (combined result: 1850106056)
0: 18.656282 ns (combined result: -1791631815)
1: 18.166277 ns (combined result: -1791631815)
0: 18.048943 ns (combined result: -2023037636)
1: 18.987328 ns (combined result: -2023037636)


I should mention that this is under WIndows 2000 workstation.  What kind of machine do you run Markus?

Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #7 - Posted 2006-02-20 14:40:41 »

Windows XP, some kind of dual core intel cpu, java 1.5.0_06

I added a third test that just does "res += vals;" for benchmarking purposes, and made it run with varying array sizes.
The combined result of the raw method doesn't match (naturally) because it doesn't abs the value at all.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
Running test with vals[10]
Math.abs(): 93.663999710083 ns (combined result: -54233294)
Foo.abs(): 82.00599899291993 ns (combined result: -54233294)
raw (no abs): 81.54599906921386 ns (combined result: -1355128174)

Running test with vals[100]
Math.abs(): 20.077400035858155 ns (combined result: -856536926)
Foo.abs(): 10.829700002670288 ns (combined result: -856536926)
raw (no abs): 11.216599912643433 ns (combined result: -829359714)

Running test with vals[1000]
Math.abs(): 15.26091004371643 ns (combined result: 224407370)
Foo.abs(): 6.911370005607605 ns (combined result: 224407370)
raw (no abs): 6.515409989356995 ns (combined result: 1516536884)

Running test with vals[10000]
Math.abs(): 14.709900970458984 ns (combined result: -1976783927)
Foo.abs(): 6.338977026939392 ns (combined result: -1976783927)
raw (no abs): 5.448884983062744 ns (combined result: -1349739601)

Running test with vals[100000]
Math.abs(): 14.452681474685669 ns (combined result: -971028899)
Foo.abs(): 5.4027831172943115 ns (combined result: -971028899)
raw (no abs): 5.539101600646973 ns (combined result: 1645818807)

Play Minecraft!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #8 - Posted 2006-02-20 14:58:48 »

Must be your CPU.  I did the same and got the same results.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
0: 19.37425 ns (combined result: -536942588)
1: 19.841349 ns (combined result: -536942588)
2: 0.001397 ns (combined result: 0)
0: 18.103975 ns (combined result: -624993057)
1: 18.235836 ns (combined result: -624993057)
2: 0.002235 ns (combined result: 0)
0: 17.929932 ns (combined result: 67879535)
1: 18.14616 ns (combined result: 67879535)
2: 0.001677 ns (combined result: 0)
0: 17.901716 ns (combined result: -242554893)
1: 18.668016 ns (combined result: -242554893)
2: 0.001676 ns (combined result: 0)

Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #9 - Posted 2006-02-20 15:00:40 »

Combined result 0 for the raw one?
That's highly unlikely.. You must've made a mistake, causing the jvm to optimise it out.

1  
2  
3  
4  
5  
6  
case 2:
    for (int i = 0; i < vals.length; i++)
    {
        res += vals[i];
    }
    break;


That's what it's supposed to look like.

Play Minecraft!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #10 - Posted 2006-02-20 15:04:20 »

Just to make sure it wasn't some kind of issue with the Math class itself, I changed Foo.abs into this:

1  
2  
3  
4  
public static int abs2(int a)
{
    return (a < 0) ? -a : a;
}


And got this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
--------------------------------------------------------------------
Test results with vals[10]: (Average of 100 runs)
  Math.abs(): 72.73299858093262 ns (combined result: -996863761)
   Foo.abs(): 72.42699935913086 ns (combined result: -996863761)
raw (no abs): 63.051000633239745 ns (combined result: 1848957913)
--------------------------------------------------------------------
Test results with vals[100]: (Average of 100 runs)
  Math.abs(): 23.246200065612793 ns (combined result: 1647719572)
   Foo.abs(): 23.09040014266968 ns (combined result: 1647719572)
raw (no abs): 13.481200141906738 ns (combined result: 2001841642)
--------------------------------------------------------------------
Test results with vals[1000]: (Average of 100 runs)
  Math.abs(): 14.749670057296752 ns (combined result: 685150735)
   Foo.abs(): 14.555380010604859 ns (combined result: 685150735)
raw (no abs): 6.045389981269836 ns (combined result: 433340265)
--------------------------------------------------------------------
Test results with vals[10000]: (Average of 100 runs)
  Math.abs(): 14.555736017227172 ns (combined result: 966036701)
   Foo.abs(): 14.627489070892334 ns (combined result: 966036701)
raw (no abs): 5.03886999130249 ns (combined result: -1169738463)
--------------------------------------------------------------------
Test results with vals[100000]: (Average of 100 runs)
  Math.abs(): 14.596469192504882 ns (combined result: -1558903801)
   Foo.abs(): 14.690511283874512 ns (combined result: -1558903801)
raw (no abs): 5.81084680557251 ns (combined result: 1243173235)

Play Minecraft!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #11 - Posted 2006-02-20 15:34:33 »

Accidentaly put case 3: instead of case 2:

Here it is.
1  
2  
3  
4  
5  
6  
7  
8  
9  
0: 19.011356 ns (combined result: 45605829)
1: 19.629032 ns (combined result: 45605829)
2: 3.061003 ns (combined result: 728343281)
0: 17.960941 ns (combined result: -1060178124)
1: 18.297018 ns (combined result: -1060178124)
2: 3.307683 ns (combined result: -840133284)
0: 18.10258 ns (combined result: 123241034)
1: 18.174376 ns (combined result: 123241034)
2: 3.410769 ns (combined result: -1528109838)


And here is a test switching the new abs with the way the Math class does it
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
0: 18.6479 ns (combined result: -178609940)
1: 19.83716 ns (combined result: -178609940)
2: 3.032229 ns (combined result: 109507854)
0: 18.042517 ns (combined result: 959397402)
1: 17.889145 ns (combined result: 959397402)
2: 2.946464 ns (combined result: 81418436)
0: 18.365185 ns (combined result: 1902673406)
1: 18.029943 ns (combined result: 1902673406)
2: 3.650184 ns (combined result: -954166960)
0: 17.775164 ns (combined result: -706146910)


Maybe it is the bitshift that people were talking about.  My CPU might not be able to do 31 bit shift as efficiently as yours.  This is also my work machine.  I will have to try this at home as well.  I have an AMD Athlon 64 3000+ at home.

Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #12 - Posted 2006-02-20 16:07:52 »

I'll try it at home as well, on my spanking new AMD 64 X2 4800+ (!).

This is fun. Cheesy

Play Minecraft!
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #13 - Posted 2006-02-20 16:09:05 »

Seems like a strange coincidence that the bitshifts would end up exactly the same speed as a branch.. hmm

Play Minecraft!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #14 - Posted 2006-02-20 16:29:01 »

I'll try it at home as well, on my spanking new AMD 64 X2 4800+ (!).

This is fun. Cheesy
I bow to your power and drool with envy at the same time. Grin

Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #15 - Posted 2006-02-20 16:35:56 »

Average:
Math.abs = 72.35221229553223
Foo.abs  = 38.08332901000976
Foo.abs2 = 64.98413646697998
raw      = 27.265814304351807

Lowest:
Math.abs = 66.32128143310547
Foo.abs  = 35.168434143066406
Foo.abs2 = 58.325008392333984
raw      = 26.240205764770508


100x1000000 (alternating) runs (and 50x1000000 warmup).

The CPU is a K7 500 and the vm is 1.5.0_06 client.

弾幕 ☆ @mahonnaiseblog
Offline Mark Thornton

Senior Member





« Reply #16 - Posted 2006-02-20 17:52:36 »

I get these results on a 3.06GHz P4

1  
2  
3  
4  
5  
6  
7  
8  
0: 15.937469 ns (combined result: -703529116)
1: 4.216863 ns (combined result: -703529116)
0: 16.258211 ns (combined result: -782838401)
1: 3.796482 ns (combined result: -782838401)
0: 15.778359 ns (combined result: -1823151896)
1: 3.805004 ns (combined result: -1823151896)
0: 15.950707 ns (combined result: 1636766866)
1: 4.353477 ns (combined result: 1636766866)


As I noted on the other thread, some P4 processors have a very slow shift operation.
Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #17 - Posted 2006-02-20 17:59:38 »

As I noted on the other thread, some P4 processors have a very slow shift operation.

That's what I was going to say, at some point the dropped the barrel shifter from the hardware, iirc.
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #18 - Posted 2006-02-20 19:23:35 »

At home, with the old code:

1  
2  
3  
4  
5  
6  
7  
8  
0: 11.317748 ns (combined result: -2119606919)
1: 3.853558 ns (combined result: -2119606919)
0: 11.536717 ns (combined result: 575031759)
1: 4.281537 ns (combined result: 575031759)
0: 11.29487 ns (combined result: -1044030673)
1: 3.956503 ns (combined result: -1044030673)
0: 11.513899 ns (combined result: -423433946)
1: 4.126713 ns (combined result: -423433946)

Play Minecraft!
Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #19 - Posted 2006-02-20 19:51:48 »

As I noted on the other thread, some P4 processors have a very slow shift operation.

That's what I was going to say, at some point the dropped the barrel shifter from the hardware, iirc.

They attempted this idiocy at Northwood. They reintroduced barrel shifter in Presscott.
 
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #20 - Posted 2006-02-21 01:01:51 »

Here is what I get on my home machine:

Windows XP Pro SP2
AMD Athlon 64 3000+
512MB RAM

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
0: 15.610084 ns (combined result: 1818930407)
1: 10.661131 ns (combined result: 1818930407)
0: 15.745297 ns (combined result: -409766598)
1: 10.656383 ns (combined result: -409766598)
0: 15.857882 ns (combined result: -1521671903)
1: 10.618668 ns (combined result: -1521671903)
0: 15.650313 ns (combined result: 1713054016)
1: 10.628446 ns (combined result: 1713054016)
0: 15.670707 ns (combined result: 372041738)
1: 10.599672 ns (combined result: 372041738)

Offline Orangy Tang

JGO Kernel


Medals: 51
Projects: 11


Monkey for a head


« Reply #21 - Posted 2006-02-21 01:08:24 »

Second page already and no word from Jeff about the perils of microbenchmarking? Shock! Horror! Wink

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #22 - Posted 2006-02-21 09:47:41 »

Foo.abs() obviously is a lot faster than Math.abs() on certain hardware. I'm positive(*) there are no serious flaws in the benchmark.

BUT; it's still totally pointless. ;-)

I'm willing to bet my left lung that Math.abs() has never in the history of mankind even been close to being a bottleneck in any kind of program.
Optimisations such as these are dangerous, pointless and a huge waste of time.

Like the 4k competition, I'm in this thread because it's fun, not because it's good programming.


(* Intentionally pushing fate. I want to be proven wrong. Cheesy)

Play Minecraft!
Offline Jeff

JGO Coder




Got any cats?


« Reply #23 - Posted 2006-02-21 10:21:07 »

Well... Ive never seen ti be a bottleneck...

But Id be hesitant to state unequivocably you couldnt coem up with a situation where it was...

And anyway its a neat as heck little hack Cool

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #24 - Posted 2006-02-21 10:38:35 »

I added a new test:

"res += vals/(i+1);"

Addition and (integer) division.
It's SLOWER than Math.abs().

1  
2  
3  
4  
5  
Test results with vals[100000]: (Average of 100 runs)
 Math.abs(): 14.751644458770752 ns (combined result: 1669388634)
  Foo.abs(): 5.717770094871521 ns (combined result: 1669388634)
   division: 18.024079723358156 ns (combined result: -2079842138)
just lookup: 6.081118493080139 ns (combined result: 1416599130)


So it's not exactly slow anyway.

Play Minecraft!
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #25 - Posted 2006-02-21 15:24:16 »

I got some interesting results on a PowerPC G4 1GHz  (Powerbook)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
0: 34.847 ns (combined result: -803147295)
1: 47.583 ns (combined result: -803147295)
0: 41.461 ns (combined result: 67463555)
1: 25.959 ns (combined result: 67463555)
0: 63.427 ns (combined result: -2075964233)
1: 26.964 ns (combined result: -2075964233)
0: 48.791 ns (combined result: -306157959)
1: 61.932 ns (combined result: -306157959)
0: 34.877 ns (combined result: -1075941364)
1: 43.152 ns (combined result: -1075941364)
0: 41.096 ns (combined result: -509972597)
1: 27.036 ns (combined result: -509972597)
0: 41.007 ns (combined result: -1027229940)
1: 26.213 ns (combined result: -1027229940)
0: 34.649 ns (combined result: -1241157218)
1: 26.231 ns (combined result: -1241157218)
0: 34.967 ns (combined result: -1301210477)
1: 25.982 ns (combined result: -1301210477)
0: 35.26 ns (combined result: -686611777)
1: 26.503 ns (combined result: -686611777)
0: 34.647 ns (combined result: 924630738)
1: 43.598 ns (combined result: 924630738)


It seems that on average Math.abs() was significantly faster.  but there are still a notable number of iterations where the reverse seems to be true.  I wonder if that has to do with the distribution of negative values in the array and therefore how often the branch was taken?

Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #26 - Posted 2006-02-21 15:47:32 »

Actually, I get the average time for 0 as 40, and the average time for 1 as 35.

Meaning that, on average, Math.abs is slower.
Of course, that's not a huge sample size. Wink


Try making a version that alternates between all negative values, all positive and random?

Play Minecraft!
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #27 - Posted 2006-02-21 16:05:29 »

Yes.. but you threw me off by putting the comments backwards in the source you posted !!!
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
                    case 0: // New abs
                       for (int i=0; i<vals.length; i++)
                        {
                            res += Math.abs(vals[i]);
                        }
                        break;
                    case 1: // Math.abs
                       for (int i=0; i<vals.length; i++)
                        {
                            res += Foo.abs(vals[i]);
                        }
                        break;
 


Smiley

Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #28 - Posted 2006-02-21 16:27:10 »

Er, oops. :-O

Haha. Let's pretend it was a demonstration on why documenting what instead of why is bad. Cheesy


Play Minecraft!
Offline g666

Junior Member





« Reply #29 - Posted 2006-02-21 21:21:45 »

Another fun question for u oldbies:

would:

return (x > 0) ? x : -x;

be faster in java than:

if(x > 0) return x;
else return -x;

?

PS>OT how do u test the accuracy of a trig function like java apparently without cause extra trig functions that are known to be accurate?

desperately seeking sanity
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (56 views)
2014-04-15 18:08:23

BurntPizza (54 views)
2014-04-15 03:46:01

UprightPath (67 views)
2014-04-14 17:39:50

UprightPath (50 views)
2014-04-14 17:35:47

Porlus (67 views)
2014-04-14 15:48:38

tom_mai78101 (91 views)
2014-04-10 04:04:31

BurntPizza (152 views)
2014-04-08 23:06:04

tom_mai78101 (248 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (212 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!