|
Hello Friends,
Please observe the code which is given below
#include <iostream.h>
int main()
{
for(int i=0 ; i<=10 ;i++ , cout << i << endl );
cout << endl;
return 0;
}
Please observe the the second which is given below
#include <iostream.h>
int main()
{
for(int j=0 ; j<=10 ;j++ )
cout << j << endl;
return 0;
}
My question is how is it possible to use cout in the inside the for loop.Is their any difference in the between the two program? Can anyone point the difference in terms of cpu speed,memory management etc.What is advantage of writing the code as you saw in the first section of the code ie the first program
Hoping for a reply.
Best Regards,
Phijo
|
|
|
|
|
1. Yes it is possible to use cout inside the for loop.
2. The fragments are not the same: the first one prints 1 through 11 with a new line at the end; the second prints 0 through 10.
3. The pseudo code for the ‘for’ loop:
for (expression1; expression2; expression3)
{
Body;
}
is somewhat like this:
EXECUTE expression1;
LABEL1:
IF expression2 is FALSE GOTO LABEL2
EXECUTE Body;
EXECUTE Eexpression3
GOTO LABEL1
LABEL2:
This pseudo code should answer why the fragments do not provide the same results. Note that the 1st fragment has an empty body.
4. From CPU usage point of view, the 1st one executes 11 'cout << i' operations and 12 times flushes the buffer (‘cout << endl’), whereas the 2nd one - 11 and 11 respectively, so the second one is faster.
5. From the code size point of view, the 2nd one is smaller as it contains 2 cout calls, whereas the 1st one – 3.
6. It is hard to say about advantages as the two are not equivalent from the point of view of producing the result.
Serge
|
|
|
|
|
well first thingz first!!!!!...
both of them should work.....
and the first example program that u have shown is a BAD style of writing the code.....
both ur programz WILL GIVE DIFFERENT OUTPUTZ!!!!!
ur first program is like thiz.....
#include
int main()
{
for(int i=0 ; i<=10 ;i++ , cout << i << endl );
cout << endl;
return 0;
}
the OUTPUT TO THE FIRST ONE SHOULD BE.....
1
2
3
4
5
6
7
8
9
10
11
OBSERVE.....
the third argument that u give in the for loop is basically executed only AFTER the whole loop is done.....since i++is incremented first the output changez and is different from the second program
the second program that u have shown ......
#include
int main()
{
for(int j=0 ; j<=10 ;j++ )
cout << j << endl;
return 0;
}
the OUTPUT TO THE FIRST ONE SHOULD BE.....
0
1
2
3
4
5
6
7
8
9
10
i dont see any advantages in writing the first way.....what i think is that itz a bad style ......
as far as memory and processor speedz concern.....i think u wouldnt find much of a any difference.....
"faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
|
|
|
|
|
An interesting question.
I would guess (but it is only a guess) that there will be no difference, i.e. the compiler will reduce them to about the same code, so the CPU speed, memory management, etc will all be about the same.
((I stand corrected, having seen the other replies. Unfortunately I could not do the obvious check - running them - since my office PC does not have C++ installed. I think the rest of the comments below are still valid, however!))
To find differences in timing, you could use both approaches, but check performance using the profiler. This may force you to use debug version, and turn off optimisation, so it may give you misleading results.
To check memory management, etc, you could compile with the "generate assembler listing" option ON, and compare the resulting listings.
|
|
|
|
|
A for statement is comprised of three expressions: init, condition, and loop. All three are optional. The init expression is evaluated once. The condition expression is evaluated once before each iteration of the loop. The loop expression is evaluated once after each iteration of the loop.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi, there,
I created Message Text File and one of the messages has 4 parameters.
MessageId = 0x1000
Facility = Application
Severity = Informational
SymbolicName = MSG_NORMAL
Language = English
%1 %2 %3 %4
.
Then I use FormatMessage function to retrive the message by ID and
pass the parameters in. But I can't get the string I want.
DWORD_PTR args[4] = { (DWORD_PTR)(&string1),
(DWORD_PTR)(&string2),
(DWORD_PTR)(&string3),
(DWORD_PTR)(&string4)};
// Retrieve the English message string.
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_HMODULE |
FORMAT_MESSAGE_ARGUMENT_ARRAY,
ghResDll,
EVENT_MSG_SESSION_NORMAL,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPTSTR) &lpMsgBuf3,
2048,
(va_list*)(&args));
Then
evtLog->LogIt( EVENTLOG_INFORMATION_TYPE, 1,
EVENT_MSG_SESSION_NORMAL, pMsg3, 1 );
So in event log is %2 %3 %4
Can anyone tell me where is wrong with my code? Thanks In Advance.
|
|
|
|
|
What does your LogIt() method do? Shouldn't lpMsgBuf3 be sent to it instead of pMsg3 ?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
LogIt() just calls EventReport function. Because it needs LPTSTR[] and lpMsgBuf3 is LPTSTR...so basically pMsg3 = {lpMsgBuf3}
Thanks for your reply.
|
|
|
|
|
So is an assignment statement missing from your original post?
Have you tried scaling the problem down to just two replaceable parameters instead of four? Have you tried using FormatMessage(..., (va_list*) args) instead?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
One of the parameters to your LogIt() should be the number of parameter strings - i.e. 4.
::ReportEventW(
hnd,
EVENTLOG_INFORMATION_TYPE,
0,
EVENT_MSG_SESSION_NORMAL,
sid,
strNbr, // should be 4, # of args
DS,
str, // args array
(void*)D
);
...cmk
Save the whales - collect the whole set
|
|
|
|
|
|
Hi
Anyone know if there is defined constant value for Data type...For example
DWORD.... Max Range For DWORD is FFFFFFFF
Need To Do Functions That Do Checking If The Passed In values Meet The Range Or Not. Although Can Hardcode the FFFFFFFF, i ma wondering if there is any better way.
Thanx
|
|
|
|
|
Yes, they are defined in LIMITS.H
Serge
|
|
|
|
|
when I capture the screen,I can't capture the mpeg file's image under playing,why??and what i should do~who can help me~thank you very much~~
Jack
|
|
|
|
|
Because no-one can. They are drawn direct to the screen.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
If you turn off 'overlay' in the hardware acceleration settings
of your video card you should be able to capture video windows
(but it may seriously impact video performance)
Steve T
|
|
|
|
|
how do i set the hardware acceleration programmatically in c# or in c++?
|
|
|
|
|
Sorry, I don't know. If it's possible it will probably
be a DirectShow function. Most likely a property of the
video renderer you are using. You may even be able to
acheive it by simply using a different video renderer
in your filtergraph.
Steve T
|
|
|
|
|
Could some of you tell me how to get the RGB value of a pixel from a frame which captured from USB camera.
|
|
|
|
|
You need to either use GetPixel ( which is slow ), or make your image a DIBSection or use GDI+ to access the bits as an array.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Thank you for your reply, could you give me some idea on it?
I have no idea..
|
|
|
|
|
In what format is the frame stored?
Assuming you used ISampleGrabber you will have a frame stored
in whatever format the video pin directly upstream of your
sample grabber is configured to. e.g 24-bit YUV ( FourCC, IYUV )
This is essentially an array of YUV values so first you will
want to access the YUV values (3 bytes) at the pixel location
Use a BYTE pointer that is set to a function of your frame
buffer start, buffer width, Pixel X and Y position and size (in bytes)
e.g. assuming 640 x 480 24-bit YUV (3 bytes), To find pixel(X,Y) :
BYTE *pPixel = pBufferStart + ( Y * 640 * 3 ) + ( X * 3 );
Now
BYTE Y = *pPixel;
BYTE U = *(pPixel+1);
BYTE V = *(pPixel+2);
This will be different if the stored frame is in another format.
Assuming you correctly get the pixel YUV values now convert
to RGB
Of the top of my head I think the formula is :
R = 1.164*Y + 1.596*V;
G = 1.164*Y - 0.813*V - 0.392*U;
B = 1.164*Y + 2.017*U
But I'm not sure. You can look it up.
In ant case if you're doing a lot of pixel testing you
should build look-up tables from those formulas at
initialization time.
Steve T
|
|
|
|
|
|
Thanks for your reply!
another question, how can I store the frame to buffer from the USB camera, and how to compare two frames if they are different?
Thanks alot!
|
|
|
|
|
Thanks for your reply!
another question, how can I store the frame to buffer from the USB camera, and how to compare two frames if they are different?
I use ISampleGrabber in a DirectShow filtergraph to get video frames
(there are other ways)
Here is the DS documentation: ISampleGrabber
There is some sample code if you click through "Using the Sample Grabber"
As to comparing two frames: what differences do you want to detect
and how sensitive to change do you want your detection to be?
In a live video stream even visually indistinguishable frames will have
at least some minor diffences due to tiny camera vibrations, subtle
changes in light-level, sensor "noise", etc.
One simple test you can do on a per-pixel basis is simply compare
the Y-values of corresponding pixels in two frames (in YUV format).
This essentially monitors for a change in brightness. (Like comparing
two gray-scale pixels)* If you are more interested in looking for
changes in a particular area of your frame you could accumulate
Y-values in that area of each frame (add the Y values for each pixel
in the area, average them or accumulate each pixel's difference--whatever
is appropriate for your application--then compare the accumulation
to see if passes a threshold, indicating the selected area of your
video frame has changed.
*If instead of converting a YUV image to RGB using the formula
I gave you simply use the Y value for R, G & B you will get a
gray-scale image.
Steve T
|
|
|
|