Challenge 11/13: Do you know your code?

Started by
64 comments, last by Evil_Greven 20 years, 5 months ago
quote:Original post by Evil_Greven
fredizzimo,
Upon further testing of that segment with random numbers, it isn''t always detecting a collision correctly.


Of course it doesn''t I told you I was and still am very tired, but change 0x10000000 to 0x80000000 and it should(I hope).

And yes, as I told in my post, I don''t expect it to be faster. It should just serve as a proof that it''s not always worth optimizing tests away. But I''m still waiting for Magmai''s results

Advertisement
Only for information. GCC 3.3.1 generates same code for both option 1 and 2 (tested), exactly...
If brute force does not solve your problem....you're not using enough!
quote:Original post by fredizzimo
quote:Original post by Evil_Greven
fredizzimo,
Upon further testing of that segment with random numbers, it isn''t always detecting a collision correctly.


Of course it doesn''t I told you I was and still am very tired, but change 0x10000000 to 0x80000000 and it should(I hope).

And yes, as I told in my post, I don''t expect it to be faster. It should just serve as a proof that it''s not always worth optimizing tests away. But I''m still waiting for Magmai''s results



An optimizing compiler will perform sub-expression elimination for you, I guess it doesn''t hurt to give it hints.
However, this does not detect any collisions.
int jx=x-10;
int jy=y-10;
return jx-ix & ix-jx & jy-iy & iy-jy & 0x80000000;

Following your logic though, I came up with this:
segement5
return !!(0x8000000 & (x-ix-10 & y-iy-10 & -x+ix-10 & -y+iy-10));

2255038 hits out of 10,000,000 (22.55%)         Avgerage     StdDevSegment1: 1.814189      0.000286Segment2: 1.814146      0.000298Segment3: 1.791043      0.000413Segment4: 1.731903      0.000383 Segment5: 1.731680      0.000309361321 hits out of 10,000,000 (3.6%)          Avgerage     StdDevSegment1: 1.795704     0.000457Segment2: 1.795874     0.000400Segment3: 1.736898     0.000481Segment4: 1.731758     0.000392Segment5: 1.731592     0.000373 


It appears to be ever-so-slightly faster, even though it''s doing much more math.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Same idea but slightly faster (at least for me but Magmai seems to be the only one with a stable benchmark

inline bool segment6(int x, int y, int ix, int iy){	ix -= x + 10,ix &= -ix - 20;	iy -= y + 10,iy &= -iy - 20;	return (ix & iy) < 0;}
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
a benchmark on that would be very much appreciated!

magmai, think you could?.. *plz*

}-- Programmer/Gamer/Dreamer --{
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
quote:Original post by C-Junkie
Who cares, it was yet another micro-optimization thread with some naieve people thinking that one piece of code might be faster than another.

I vote that anyone who posts a micro-optimization thread is banned from now on.
I vote that anyone who thinks this should be banned. This stuff isn''t useless. How do you think your compiler does micro-optimizations? Someone has to write that compiler. How would anyone do that if every person who was interested in optimization was banned or forgot to use their brain like you? Some people just happen to think it''s interesting as hell to find clever ways of doing things. Please don''t be an idiot bandwaggoner, spouting crap you heard from someone you don''t know without stopping to think about whether it''s true or whether it applies to the situation.

This topic is closed to new replies.

Advertisement