Revers order of bytes unsigned char array :?

Dec 1, 2014 at 10:19pm
hi,

i need fastest method to revers order of bytes in my char array.

for example i have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
unsigned char buf[8];
// consider data stored in buf is 88 77 66 55 44 33 22 11
// how to reverse it to: 11 22 33 44 55 66 77 88 

// currently i can do it by equal assignment , i make another buf like:

unsigned char buf_ok[8];
 
        buf_ok[0]=buf[7];
	buf_ok[1]=buf[6];
	buf_ok[2]=buf[5];
	buf_ok[3]=buf[4];
	buf_ok[4]=buf[3];
	buf_ok[5]=buf[2];
	buf_ok[6]=buf[1];
	buf_ok[7]=buf[0];

// This does revers the bytes as i want but its very slow , i am looking for fast method ..
Dec 2, 2014 at 9:52am
hi,

anyone please know better and fast method ?
Dec 2, 2014 at 10:09am
Slow? You compiled with optimizations enabled when you came to this conclusion?
Dec 2, 2014 at 10:11am
hi,

sorry i am new to c++ dont know how to compile with optimization i use ubuntu ssh cli to compile .. g++ myprogram.c -o program

Dec 2, 2014 at 10:16am
pass -O2 (or -O3) to the compiler and see if there is a difference.

g++ -O2 myprogram.c -o program
Dec 2, 2014 at 11:42am
thanks i test and find execution time ... i am running a for loop that generate bytes , using this above assignment i am able to generate 30,000 8 bytes of keys / second i want to increase speed ...
Dec 2, 2014 at 12:09pm
WOOOW compiling with -O3 result now : KEYS/SECOND: 93657

without -O3 30,000 and as program runs more time the speed droping ..


-O3 did the job thanks Peter87 :)

Dec 2, 2014 at 12:16pm
removing printf from for loop speed rised upto : KEYS/SECOND: 131120

Thank you brother for suggesting -O option :)

i will try using threads and multiple process to speed up more :) thanks again ..
Last edited on Dec 2, 2014 at 12:17pm
Dec 2, 2014 at 12:22pm
-Ofast more faster then -O3 :D
Dec 2, 2014 at 1:20pm
I don't know how you are testing this but note that if the you don't use the result of the code you are testing the compiler might optimize away too much. For example, in the code you posted, if the buf_ok array is never used later in the code the compiler could simply ignore lines 7-16.
Dec 2, 2014 at 1:58pm
The results are by using buf_ok array and assignments as mentioned in 1st post.

i wanted alternate way to reverse the bytes order , in program i think i have big endian and littel endian formate problem with revers the input array i am researching on it , finding different ways to speed up my program ...

currently the program read my array all functions works correctly , it dont ignor any line ..

Thank you peter :)
Topic archived. No new replies allowed.