Hi all,
I want to know the optimization code for following c source code:

int i=0;
i=p*q<<12;

if(i>20)
i=15;

so here i should range from 0 to 15 but if i goes beyond 20 then it should remain as 15.
Here I have hint as to use bitwise operator to optimize like i=(p*q<<12)& 0x000F;

Also please anyone can tell me the ebooks or links or good book to start with the optimization of c source code.
I am the beginner in the C source code optimization and speech coders.


Please help me out to optimize the source c codes...
also different tricks to optimize the arithmetic operations any links please help...


Sushant

There isn't exactly a whole lot there to bother optimizing. And be sure that the code you're working on is worth optimizing. Most of the time people try to optimize their code, they end up worrying about details that hardly affect the software's performance.

anyway can u please tell me the books for it??

Maybe

int i = p * q << 12;
int j = (i >> (sizeof(int)-1) * CHAR_BIT) & (~0xF)
i = (i & j) | (~ j);

Maybe

int i = p * q << 12;
int j = (i >> (sizeof(int)-1) * CHAR_BIT) & (~0xF)
i = (i & j) | (~ j);

this would be about twice as slow as the original code.
and the code size would also be close to double the original.
(unless the compiler is smart enough to rewite it.)

>I want to know the optimization code for following c source code
So you've profiled your code and found the code you posted to be a bottleneck? If you haven't done that, you have no business optimizing it.

Come to think of it, it's not even correct. However, the code I gave (despite its uselessness) is only 3 bytes longer than the OP's code when compiled under GCC -O3.

The using-bitwise-operators-to-crazily-remove-the-branching-instruction idea is useless anyway -- that gets compiled down to a conditional move.

click me
Write clear code, use the best algorithms and data structures for the problem (do some research) and profile your code to find out where the real issues are.

And leave all the low level micro-optimisation bit magic to the compiler.

oh no I will give u simple one more code similar to it...
int i;
i=j>>8;
if(i>63)
then i=63;
so over how one can do without using if statement? please try to help me since this type of code is used in so many cases in my big project......


please help!!!

>please try to help me since this type of code is used in so many cases in my big project......
Yea, if statements are rather common. :icon_rolleyes: I'll ask again. Have you profiled your code and determined that this exact line of code is causing a significant slowdown in your application? If not, you're trying to optimize without all of the facts, and that's tantamount to wasting your time.

void foo ( int j ) {
    int i;
    i=j>>8;
    if(i>63) i=63;
    printf("%d\n", i );
}

_foo:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	movl	8(%ebp), %eax
	sarl	$8, %eax
	cmpl	$63, %eax
	jle	L2
	movl	$63, %eax
L2:
	movl	%eax, 4(%esp)
	movl	$LC0, (%esp)
	call	_printf
	leave
	ret

It's only 4 instructions, just how many fewer do you think you can get it down to?

Like I said earlier, work on the problems which the compiler knows nothing about, like replacing bubble sort with quick sort (perhaps).

Fiddling with random single lines will not get you a 10x improvement.
Finding the wasteful algorithms and useless functionality will.

Which OS/Compiler are you using?
Do you know what a profiler is?
Do you have real world data and a finished program?

just to reinforce what narue and salem have stated (more than once), here is a quote from someone who has dedicated a lifetime on doing things efficiently (and elegantly) in code:
"Premature optimization is the root of all evil (or at least most of it) in programming." - Donald Knuth

Thanks all but can please tell me the books to study various optimizatons in c source code.... also u all are right since I am just messing around only one if statement.... so i will try to understand my whole project and the algorithms...
THANKS A LOT...
please give me some starting books to get the different optimization techniques.....

Optimization techniques: link link For the second one, pay attention to the sections titled "Bottlenecks" and "When to optimize" since those don't seem to be sinking in

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.