Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kingpinzs

need help speeding up a simple programe.

This topic is 5565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I made a sub prgrame for my dos sim .
 int a = 0,b = 0,c = 0 ,d = 0;
  
 
  
    do
  {
  a = a + 1;
  
   
  if (a == 255)
  {
  b = b + 1;
  }
  
  if (a >= 255)
  {
  a = 0;
   }
   
   if (b == 255)
   {
   c = c + 1;
   }
   
   if (b >= 255)
   {
   b = 0;
   }
   
   if (c >= 255)
   {
   c = 0;
   }
   
   if (c ==255)
   {
   d = d + 1;
   }
   
   
     }
  while(d != 256 );
Is there any way to speed this up? It takes over an hour to finsh. I would like it to take less then 1/2 an hour to run. Thanks for any Help.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by kingpinzs
I made a sub prgrame for my dos sim .

Is there any way to speed this up? It takes over an hour to finsh. I would like it to take less then 1/2 an hour to run.

Thanks for any Help.



You don't need to check all the variables every loop - you can nest them e.g.


int a = 0,b = 0,c = 0 ,d = 0;



do
{
a = a + 1;


if (a == 255)
{
b = b + 1;
if (b == 255)
{
c = c + 1;

if (c ==255)
{
d = d + 1;
c = 0;
}
b = 0;
}
a = 0;
}

}
while(d != 256 );


The reason for this is you only need to check value b/c when it could have potentially changed i.e. when you add one to it.

Edit:
This looks like a bug to me:


if (c >= 255)
{
c = 0;
}

if (c ==255)
{
d = d + 1;
}

The second statement should come first, otherwise d will never change.

Also the >= comparisons are redundant see modified code.

[edited by - jamessharpe on September 21, 2003 5:32:37 PM]

Share this post


Link to post
Share on other sites
hmm i think you''ve removed some part (you dont want to show it, otherwhise i dont see why you should do it...)


how about:

unsigned int tmp= 0;
do{
tmp++;
int a= tmp & 0xFF;
int b= tmp>>8 & 0xFF;
int c= tmp>>15 & 0xFF;
int d= tmp>>24 & 0xFF;
}while(tmp);


???

T2k

Share this post


Link to post
Share on other sites
quote:
Original post by T2k


unsigned int tmp= 0;
do{
tmp++;
int a= tmp & 0xFF;
int b= tmp>>8 & 0xFF;
int c= tmp>>15 & 0xFF;
int d= tmp>>24 & 0xFF;
}while(tmp);





Can you pleas explain your code a little?
what is 0xff and what do the numbers do or stand for?

Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by T2k
hmm i think you''ve removed some part (you dont want to show it, otherwhise i dont see why you should do it...)


how about:

unsigned int tmp= 0;
do{
tmp++;
int a= tmp & 0xFF;
int b= tmp>>8 & 0xFF;
int c= tmp>>15 & 0xFF;
int d= tmp>>24 & 0xFF;
}while(tmp);


???

T2k


He creates an integer with no +/- sign for temporary storage. He initiates the do loop, adds 1 to tmp, and then makes a = tmp refrenced with the address 0xFF. He makes b = tmp shifted to the right 8 times, and refrences it to 0xFF. He does this with C 15 times, and D 24 times while tmp is not equal to zero. The ''&'' gives it the value of the adress 0xFF. Note that this loop will expire at some point, as shortly after 37 billion it goes down to negative 37 billion, but since it is unsigned, it will go to 74 billion and then reset to zero, making it false. I hope I explained this correctly.

Scott Simontis
e-mail:ageofscott@comcast.net
AIM:ssimontis

Share this post


Link to post
Share on other sites
when c hits 255 it just restarts c and does not go on to d. why whould that be?


unsigned int tmp= 0;
do
{
tmp++;
int a= tmp & 0xFF;
int b= tmp>>8 & 0xFF;
int c= tmp>>15 & 0xFF;
int d= tmp>>24 & 0xFF;}
while(tmp);

Share this post


Link to post
Share on other sites
I think it''s a little Typo at shifting the tmp-variable at int c= ...


unsigned int tmp= 0;
do
{
tmp++;
int a= tmp & 0xFF;
int b= tmp>>8 & 0xFF;
int c= tmp>>16 & 0xFF; // <- 16 not 15 bit''s to shift

int d= tmp>>24 & 0xFF;
}while(tmp);


For the explanation what it does:

tmp++;
stands for:
tmp = tmp + 1;

By Shifting tmp with 0,8,16 and 24 and Masking with 0xFF you get the Bytes 1 to 4 of the tmp-Variable.

The >> Commands shifts the Bits in "tmp" by ">>X" Bits.

Than the & Command with 0xFF is beeing used to Mask the lower 8 Bit of the shiftet tmp-variable. The 8bit are exactly the number of bits to hold the desiered 0 to 255 numbers.

The &-Command stands for Bitwiese AND:

Bitwise AND is defined as:

Bit A: Bit B:Result
0 : 0 : 0
0 : 1 : 0
1 : 0 : 0
1 : 1 : 1

Hexadecimal 0xFF is the Bitcombination (as a 32bit unsig. Int):
00000000000000000000000011111111

So 0xFF let''s only the lower 8 Bits of an Integer go into the Result.
When the tmp Reaches 0xFFFFFF and a nother 1 is added it rolles over to 0x00000 so the While-Loops ends.

I hope it explanes how this Problem kann be sollved with bit-Operations.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!