Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Faster, shorter
cybernetix
post Oct 25 2004, 11:28 AM
Post #1


ReloadeD
Group Icon
Group: Honored
Posts: 562
Joined: 20-May 04
From: Belgrade, Serbia
Member No.: 17





how many times you've got calculations like this:

z=Math.round((x+y)/2);

usually I use it to calculate new position for objects that shuold be on integer _x or _y, like texts or small images (to avoid blur).

so, what computer do in this case?

first operation: x+y
second operation: first_result / 2
third operation: Math.round( second_result )
fourth operation: z = third_result

and thats it.

can we do it faster ?

yes.

how?

bitwise shifting!

z=x+y>>>1;

in this case operations are:
first operation: x+y
second operation: first_result>>>1
third operation: z = second_result


one step less, 25% faster.


bitwise shifting directly change individual bits in an operand, so it is so fast that it could not be faster. also, what is important, allways returns integer, so - it is not neccessary to round() it.

in this case it is like this:

first_result >>>1 means DIVIDE first_result WITH 2¹

first_result >>>2 would means DIVIDE first_result WITH 2²

first_result >>>n would means DIVIDE first_result WITH 2n

etc

IMPORTANT: works only with positive numbers.
 
+Quote Post  Go to the top of the page
cmonney
post Oct 25 2004, 11:54 AM
Post #2


~ Soul Rebel ~
Group Icon
Group: Main Team*
Posts: 3,625
Joined: 18-May 04
From: a mind-altering psychedelic trip
Member No.: 3





cool 'agent'... wut about this one???:


CODE
if(true){
  do this;
}else{
  do that;
}


or

CODE
true ? do this : do that;



bigwink.gif
 
+Quote Post  Go to the top of the page
cybernetix
post Oct 25 2004, 12:09 PM
Post #3


ReloadeD
Group Icon
Group: Honored
Posts: 562
Joined: 20-May 04
From: Belgrade, Serbia
Member No.: 17





if you asking about speed, there is no difference, but sometimes it is more practical if you use ternary operator, thats the name of it bigwink.gif
 
+Quote Post  Go to the top of the page
*D*
post Oct 25 2004, 12:43 PM
Post #4


Merry KissMoose
Group Icon
Group: Main Team
Posts: 15,990
Joined: 18-May 04
From: North Pole
Member No.: 2





You two need a private room lol


I'm reading this, none of it makes any sense to me, but thanks Cybernetix smile.gif
 
+Quote Post  Go to the top of the page
cmonney
post Oct 25 2004, 01:07 PM
Post #5


~ Soul Rebel ~
Group Icon
Group: Main Team*
Posts: 3,625
Joined: 18-May 04
From: a mind-altering psychedelic trip
Member No.: 3





QUOTE(cybernetix @ Oct 25 2004, 03:09 PM)
if you asking about speed, there is no difference, but sometimes it is more practical if you use ternary operator, thats the name of it bigwink.gif

gotcha... shades.gif




+++to D+++

wut's up D... weren't you out for 3 hours??? ohmy.gif
 
+Quote Post  Go to the top of the page
e.brahim
post Dec 3 2004, 07:24 AM
Post #6


SPAM MASTER
Group Icon
Group: Member
Posts: 5
Joined: 2-October 04
Member No.: 803





you guys are too high for me
 
+Quote Post  Go to the top of the page
MAngeles
post Mar 17 2006, 09:52 AM
Post #7


New Member
Group Icon
Group: Member
Posts: 5
Joined: 17-March 06
Member No.: 10,339





Original

Thanks
 
+Quote Post  Go to the top of the page
Guest_esmer67_*
post Apr 23 2009, 10:56 PM
Post #8




Guests






thanks
 
+Quote Post  Go to the top of the page
Andrew_F
post Nov 5 2009, 04:08 AM
Post #9


New Member
Group Icon
Group: Member
Posts: 20
Joined: 12-April 09
Member No.: 24,863





z=x+y>>>1;
never used ">>>" before...
Need to consider it more carefully//
Thanks for idea!!
 
+Quote Post  Go to the top of the page

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 29th July 2010 - 10:59 PM
Anime | Home Insurance | WoW Gold | Car Insurance Quotes | Bad Credit Loans