Skip to main content
GameDev.net gamedev.net
🔒 Locked

How could I optimize this code?

Started by darkforest Jun 1, 2006 at 2:50 PM 12 replies 1.8k views
Original Post
darkforest
darkforest

void Byteswapping(unsigned char* aDest, unsigned char* aSource, int size)
{
  int i, j;
  int iTimesSize;

  for (i=0; i<size; i++)
  {
    iTimesSize = i * size;
    for (j = 0; j<size; j++)
    {
      aDest[iTimesSize+j]=aSource[j*size+i];
    }
  }
}
Is there a easy way to optimize this?
darkforest
darkforest
void Byteswapping(unsigned char* aDest, unsigned char* aSource, int size){  aDest = aSource;}


Whould this mean the same?
rip-off
rip-off
Quote:
Original post by darkforest
void Byteswapping(unsigned char* aDest, unsigned char* aSource, int size){  aDest = aSource;}


Whould this mean the same?


Nope.

It only changes the local value of aDest.

Perhaps memcpy, or std::swap if you are using c++?
Fruny
Fruny
Quote:
Original post by darkforest
Is there a easy way to optimize this?


Not that I can see. Your compiler probably already does strength reduction and loop unrolling for you.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
dbzprogrammer
dbzprogrammer
What is the goal of your function? I'm messing around with it, and it's not really copying a C string correctly... Are you trying to flip bytes in memory, so that a C string would be spelled backwards?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
dbzprogrammer
dbzprogrammer
He'd have to flip his code around, it'd work more like this then:

void Byteswapping(unsigned char* aDest, unsigned char* aSource, int size){  int i, j;  int iTimesSize;  i = 0;  while(i<size)  {    iTimesSize = i * size;    j = 0;    while(j<size)    {      aDest[iTimesSize+j]=aSource[j*size+i];      ++j;    }  ++i;  }}


Besides, I have a feeling that leaving it as a for loop would be faster due to automatic compiler optimizations.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
BlueHabu
BlueHabu
MMX instuctions will copy things really fast if you have that option

Quick google result
http://webster.cs.ucr.edu/AoA/Windows/HTML/TheMMXInstructionSeta2.html
dbzprogrammer
dbzprogrammer
Quote:
Original post by Fruny
Quote:
Original post by darkforest
Is there a easy way to optimize this?


Not that I can see. Your compiler probably already does strength reduction and loop unrolling for you.


I don't quite get the concept of Strength reduciton, could you explain it?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
JohnBolton
JohnBolton
What is your function supposed to do? It looks like it "transposes" a size by size array of bytes.

If size is a multiple of 4, then you could speed it up by reading or writing 4 bytes at a time.

BTW, the iTimesSize variable is unnecessary. Any decent compiler will do that for you.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Scourage
Scourage
Quote:
Original post by Anonymous Poster
erm third time lucky

Normally pre increment is faster than post increment.(machine dependent optimization)


Generally true, but most importantly on expense items to create (such as iterators ). On most architectures, simple types such as ints, produce similar code for post and pre increment.

Cheers,

Bob

[size="3"]Halfway down the trail to Hell...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.