|
thanx, but i need fast, not elegant
such construction works even slowly.
|
|
|
|
|
Try it and benchmark it. I can say nearly for certain that little block of code is nowhere near a slight resemblance of your bottleneck. (of course that is if a and b are bytes or ints as you lead me to believe) Rule number 1 of optimization: Don't do it. Rule number 2: Don't do it _yet_.*
So... Unless a profiler is telling you your code is bottle-necking at that small comparator block, I suggest you worry more about the rest of your code and just leave what you have. Things like passing function args by value rather than by reference, useless construction and destruction of temporary objects derived from complex class heiarchies, and other such subtle things are nearly 100% of the time your bottleneck. (Save for things like disk/network IO, etc.)
*Herb Sutter - More Exceptional C++
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
This block processing takes 3/4 of all code time.
|
|
|
|
|
Kolich wrote:
This block processing takes 3/4 of all code time.
no way! (unless your total processing time is like 0.000012 seconds). How long is your processing time? How many elements in your matrix? Here is the disassembly for my test block:
if (a > 0)
00411ADE mov eax,dword ptr [a]
00411AE1 cmp dword ptr [eax],0
00411AE4 jle test+48h (411B08h)
if (a > 255)
00411AE6 mov eax,dword ptr [a]
00411AE9 cmp dword ptr [eax],0FFh
00411AEF jle test+3Ch (411AFCh)
b = 255;
00411AF1 mov eax,dword ptr [b]
00411AF4 mov dword ptr [eax],0FFh
else b = a;
00411AFA jmp test+46h (411B06h)
00411AFC mov eax,dword ptr [b]
00411AFF mov ecx,dword ptr [a]
00411B02 mov edx,dword ptr [ecx]
00411B04 mov dword ptr [eax],edx
else b = 0;
00411B06 jmp test+51h (411B11h)
00411B08 mov eax,dword ptr [b]
00411B0B mov dword ptr [eax],0
that's like 16 instructions! It probably takes more just to instantiate one of your matrix objects...
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
basic time takes operation b=a.
its bad.
|
|
|
|
|
you find this elegant ?????????
i agree the ?: operator is elegant when you have few (less than 2) conditions, but it becomes really unreadable otherwise.
more, it uses exactly the same time as the previous post's code. what i prefer is expanding the code, but placing the { }. it doesn't eat bread and it is really more easy to debug.
one last thing. the first written code is (by my thinking quite fast). you can so let it like this :
<font style="color:blue;">if </font>(a > 0) {
<font style="color:blue;">if </font>(a > 255) {
b = 255;
}
<font style="color:blue;">else</font> {
b = (BYTE)a;
}
}
<font style="color:blue;">else </font>{
b = 0;
}
TOXCCT >>> GEII power
|
|
|
|
|
|
Nitron wrote:
I see you are a same-line bracer
where, which ? what is wrong in my code ?
i don't exactly understand your sentence. could you rephrase it please ?
TOXCCT >>> GEII power
|
|
|
|
|
You do this:
if (a > 0) {
if (a > 255) {
b = 255;
}
else {
b = (BYTE)a;
}
}
else {
b = 0;}
instead of this:
if (a > 0)
{
if (a > 255)
b = 255;
else
b = (BYTE)a;
}
else
b = 0;
i.e. you put your opening braces on the same line as your conditional statement.
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
and so what ??? didn't you ever saw that ?? is it a code fault ? pfffffff poor guy.
moreover, i said that it was more readable to write the code with the braces. when you cat multiple if statements, we are not sure to whose if the esle is associated with.
My code is correct, if you don't like this, it's your point of view, but it is efficient !
TOXCCT >>> GEII power
|
|
|
|
|
dude, chill. The code is fine, I was just giving you a hard time. I guess I know how to push your buttons
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
If you can also provide us with the data types of a and b , then may be we can find out some idea. And if you can explain what you are actually trying to do, that would be much better.
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
b is BYTE, a is float.
I'm making convolution of 2 matrix. they are BYTE type. So i need to control bounds.
|
|
|
|
|
|
|
No! its useless
|
|
|
|
|
he asked for fast, not undebugable unreadable useless code
TOXCCT >>> GEII power
|
|
|
|
|
If you really think optimizing this code is worth the effort, you couldn't be more wrong. For a detailed analysis on this topic, see this article. I'm not saying that it isn't possible, but in the grand scheme of things, saving a handful of operations is pointless.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
If these lines of code are taking most of the time and the algorithm is correct, then yes, he should look at these lines.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Try:
b = (BYTE)a;
(void)(!(a > 0xFF)||((b = 0xFF)));
|
|
|
|
|
|
Chances are most of your time is spent in "b = (BYTE) a". This is probably using "ftol" which is very slow.
Because of the C/C++ standard, the ftol must change the FPU settings before and after converting the value to an integer. Thus there is a LOT of wasted time.
Search google for "fast ftol".
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I agree it definately will be this conversion.
Ant.
|
|
|
|
|
I have seen this kind of problem before, where when manipulating matrix values, an inordinate amount of time is spent during the conversion of data types: floats to ints, for example. I hope your compiler isn't keeping "255" as an int and converting it to "255.0f" for every comparison. Certainly the compiler should be smarter than that.
If all the "a" values are floats, then your conversion to BYTE doesn't maintain any of the decimal portions of the "a" values. Therefore, why does "a" need to be a float? Could everything else be made to work properly if "a" was not a float?
You may also consider a simpler comparison structure that eliminates the "else" statements. Using "else" complicates the control flow, such that during a pre-fetch lookup, the CPU can't know which path it is going to take. When I was writing code for a DSP, this was a big concern. A statement like if (a < 0) b = 0; was able to be optimized better than if (a > 0) {...} else b = 0; because of the single statement (not a block) and not having an "else". I don't know if this strategy will help you on a normal PC.
Depending on the constraints on "b", you could try:
b = (BYTE) a;<br />
if (a < 0.0f) b = 0;<br />
if (a > 255.0f) b = 255;
At least on a DSP, while it seems that a test may be performed unnecessariyly, it was actually faster because of the improvements in control flow.
I don't know what tool you are using to test your performance, but you may want to try the Memory Validator that is advertised here on CodeProject.
Good luck.
Dave
"You can say that again." -- Dept. of Redundancy Dept.
|
|
|
|
|
With the above proposition you are always executing
b = (BYTE) a;
This is the bottleneck within the code segment. I know it will all depend on the values of a as to how relevant this would be, but it is a factor.
Ant.
|
|
|
|