Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

for loops speed.

Status
Not open for further replies.

aibelectronics

New Member
I was just wondering how fast does c do successive for loop executions?
for example:

for(i=0; i<max; i++)
{
printf("%s", "electro-tech-online");
}

what is the time gap b/w two successive prints of electro-tech-online?
 
All depends on the thread priorty from the Operating system

IF it was run in DOS it would be pretty damb fast, almost CPU speed.

Also depending on the value of "max" in the FOR loop it is actually faster to "unroll" the loop. In Linux I use Gentoo and I use the unroll-loop CFLAG there is a marked improvement for loops that are less then 15 iterations, more then the loops is just more efficient.

If you really want speed you need to write in assembler to get direct CPU access + access to CPU registers
 
reply

Joel Rainville said:
Styx said:
All depends on the thread priorty from the Operating system

IF it was run in DOS it would be pretty damb fast, almost CPU speed.

That's a very good answer to a very ambiguous question. ;) :D

What i wanted to do is to use a 'for' loop as a controlling signal for
'my' sample and hold circuit. The s/h capacitor has an acquisition time of 20us and obviously the for loop must execute for a period > 20us to enable the capacitor adequately charge up to the analog voltage.

still on the same circuit:
https://www.electro-tech-online.com/custompdfs/2005/10/circuit1_oscilloscope-1.pdf, does anyone understand how the no.s 7000 and 800 in function 'main' got, was it arbitrary?

another curious fact is that the second for loop in 'main' is a slave to the first for loop. the first for loop executes through 800 cycles while the second executes through 7000 cycles, how is that? Methinks the second for loop would stop as soon as the first stops?
thanks guys. :)
 
Re: reply

aibelectronics said:
What i wanted to do is to use a 'for' loop as a controlling signal for my sample and hold circuit. The s/h capacitor has an acquisition time of 20us and obviously the for loop must execute for a period > 20us to enable the capacitor adequately charge up to the analog voltage.

As suggested by Styx, the operating system plays a role, but also your CPU speed, the number of already running programs... Trying to figure out the "speed" of a for loop makes little or no sense, and in a good digital design is irrelevant.

What does your circuit look like? There might be an easy way of obtaining the results you're looking for?...
 
reply

thanks for the reply.
the circuit i'm trying to implement is in the link provided above, a pc-based oscilloscope.
 
Re: reply

aibelectronics said:
thanks for the reply.
the circuit i'm trying to implement is in the link provided above, a pc-based oscilloscope.

Sorry, I thought you were trying to reuse part of the PC oscilloscope C code in your own project.

Well, without having studied the circuit too closely and being fairly new to electronics, I shouldn't comment too much on it, but to me the answer is in the first paragraph of the accompanying text :

"The hardware is used to condition the input waveform and convert it to the digital format for interfacing to the PC."

So it sounds like the for loop is only there for reading the board's ADC, while the 20ns sampling is accomplished in hardware... I don't think the reading of the ADC has to be perfectly synchronized with the actual sampling. Is this what you were worried about?
 
reply

check this one out, it's almost on the last line:

'the sampling time is decided by the for loop that uses the samp value...'

it seems to me that the time base of the oscilloscope is also decided by the for loop? if i'm correct then it means the time b/w successive for loops is 1msec (too big :? ) as the time scale (x-axis) has been callibrated as 1unit=10msec i.e 10 cycles x 1msec =1unit.
 
Ok, I think get it :lol: What you need to do is adjust samp's value to obtain a delay of >20ns on *your* computer, and preferably quite a bit more than 20ns to make it a bit more reliable, keeping in mind that, from the text : When the sample value is increased, the number of points on the input signal decreases and therefore the accuracy decreases.

If it does indeed work like I described above, the overall design is pretty so-so to say the least. Any kind of process that launches on your computer (antivirus scan, document printing, windows update, etc...) might throw off the readings. Also, the samp value that gives you a decent sampling time today might not work anymore next month, and you'll have to recompile the code with an adjusted value, or at least modify the code so that samp can be adjusted by the software user...

It's still a nice project in my humble opinion, one that I'm keeping on my computer for now, that I might build in the near future :)
 
reply

20us you meant to say? well yes sure the idea as a whole is a nice one but how it was implemented (the hardware) wasn't so...one wonders for example what the LM3914 is supposed to be doing there as a zero-crossing detector...The thought of altering the sample value, if you're correct, is also not to its credit, but as you said prompting the user to enter a value for samp might do the trick.

I've got all but few of the components and should start work shortly :)
 
reply

the on/off cycle via the parallel port lasts about 1us
**broken link removed**

THAT
was what I wanted to hear :D , but perhaps I didn't express myself very well. :(
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top