473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Code optimization of inner loop

Dear all,

I would appriciate any input you may have on how to optimize this line which appears in my most inner loop:

*pG=(short)(((int)w1*V1+w2*V2+w3*V3+w4*V4)/(w1+w2+w3+w4));

w1,w2,w3,w4,V1,V2,V3,V4 are all short and the result is also short.
If it makes any difference, all variables are positive and smaller than 4095.

Thanks for any input,
Eli.
Sep 5 '06 #1
2 2400
If all the variables are initialised with the value = 4094, the value of

(int)w1*V1+w2*V2+w3*V3+w4*V4)

will exceed the maximum number that an integer can store and so the result will be a negative one.

So you can change the typecasting to float. Now the statement will become

*pG=(short)(((float)w1*V1+w2*V2+w3*V3+w4*V4)/(w1+w2+w3+w4));
Sep 6 '06 #2
This is not a problem since this case cannot happen.
Let me not bother you with the details but just say that I don't have range problems and my only problem is how to speed up this code.
Sep 6 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: junky_fellow | last post by:
What are the basic guidelines to write an optimised code. What points should one keep in mind for this ? Is this purely architecture or complier specific ? Are there any general techniques that...
11
by: syed | last post by:
I want clues to optimize a function that copies elements of a 2-dimensional array to another. Is there a way to optimize this piece of code so that it runs faster? #define RIDX(i,j,n)...
10
by: MariusI | last post by:
I stumbled over an optimization (or lack of one, to be specific) when viewing IL opcodes generated by the compiler using ms .net 2003. I was testing fast pixel manipulation using Bitmap.LockBits...
2
by: elein | last post by:
Yes, I vacuumed. Reproduced on both 7.3.2 and 7.5. Brain dead java beans want order by clauses in views that they use. my view is: select .... from bigtable b left join lookuptable l order...
10
by: Mike | last post by:
Is it still true that the managed C++ compiler will produce much better opimizations than the C# compiler, or have some of the more global/aggressive opimizations been rolled into the 2005...
19
by: felixnielsen | last post by:
Some might remember that i, not so long ago, started a treath or two about a weird 3d labyrinth. I now have a working code, that i want to share, hear comments, advice, ect., but first let me...
15
by: kenneth | last post by:
I was trying to use multiple thread to optimize my following code, but met some problems, anyone can help me? k are initialized. int computePot() { int i, j; for( i=0; i<500; i++ ) { for(...
3
by: Jeff | last post by:
Hello Sorry if this isn't entirely a C language question - perhaps someone could suggest a more appropriate group? I'm running the appended code on a Mips R12000 processor and am getting very...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.