To the question of speed and its measurement in Arduino
3r3152. 3r3-31.
3r3138. 3r3152. The necessary note - I understand perfectly well that to measure the execution time of commands, you should use more complex programs, but for a rough estimate it is quite enough and such that will be demonstrated later. 3r3138. 3r3152. 3r3138. 3r3152. So, time is changing, and very significantly, looking for the causes of this phenomenon. First of all, we pay attention to the multiplicity of the obtained values, look at the description of the work with the time library and see that 4 microsec is a measurement quantum, therefore it is better to go over to the quanta and understand that we get 4 or 5 (very often) and 6 or 7 or 8 (very rarely) units of measurement. With the first half, everything is easy - if the measured value lies between 4 and 5 units, then the spread becomes inevitable. Moreover, considering the counts to be independent, we can increase the measurement accuracy by statistical methods, which we do, obtaining acceptable results. 3r3138. 3r3152. 3r3138. 3r3152. But with the second half (??8) things are worse. We found out that the spread does not correlate with the initial data, which means that this is a manifestation of other processes that affect the execution time of commands. Note that emissions are quite rare and are not significant for the calculated average value. It would be possible to ignore them altogether, but this is not our style. In general, over the years in engineering, I realized that it was impossible to leave neponyatki, no matter how insignificant they seemed, because they have the disgusting tendency to beat in the back (well, or even reach out) at the most inopportune moment. 3r3138. 3r3152. 3r3138. 3r3152. Begin pushing hypothesis 1 - the most convenient (in terms of convenience and versatility, it is second only to the direct intervention of the Creator) - software glitches, of course, not mine, my programs never buggy, but plug-in libraries (compiler, operating system, browser, etc. - substitute ). Moreover, since I run the program in the emulator on www.tinkercad.com , you can still refer to the emulator bugs and close the topic, because the source code is not available to us. Cons of this hypothesis:
3r3152. 3r3138. 3r3152. 3r3333. 3r3152.
From cycle to cycle, the location of deviations varies, which hints. 3r3342. 3r3152.
This site still supports AutoDesk, although the argument is weak. 3r3342. 3r3152.
"We have accepted the postulate that what is happening is not a hallucination, otherwise it would simply be uninteresting." 3r3342. 3r3152.
3r3138. 3r3152. The following assumption is the effect of some background processes on the measurement result. It seems to be doing nothing, except as we believe, although we also output the results to Serial. Occurs hypothesis 2 - The output time is sometimes (strangely like that but everything happens) is added to the command execution time. Although it is doubtful how much of that output is there, but all the same - we add Flush and it did not help, we add a delay to the conclusion of the output and it didn’t help, we generally bring the output out of the cycle - still time jumps - this is definitely not Serial. 3r3138. 3r3152. 3r3138. 3r3152. Okay, what remains is the organization of the cycle itself (with what a fright it is to change its duration, it is not clear) and that’s all although micros () remained. I meant that the execution time of the first call of this function and the second one is the same when subtracting these two values, I get zero, but if this assumption is wrong? 3r3138. 3r3152. 3r3138. 3r3152. Hypothesis 3 - sometimes the second time reference is performed longer than the first one, or the actions related to time measurement sometimes affect the result. We look at the source code of the function of working with time (arduino-???hardwarearduinoavrcoresarduinowiring.c - I have repeatedly expressed my attitude to such things, I will not repeat) and see that 1 time out of 256 cycles of hardware increasing the younger part of the counter, an interrupt occurs to increment the older part counter. 3r3138. 3r3152. 3r3138. 3r3152. Our cycle execution time is from 4 to ? so we can expect 170 * (45) /256 = from three to four anomalous values on a segment of 170 measurements. We look - it is very similar, there are really 4 of them. To separate the first and second reasons, we do the calculations with a critical section with forbidden interrupts. The result does not change much, outliers still take place, which means that extra time introduces a call to micros (). Here we can not do anything, although the source code is available, but we cannot change it - libraries are included in binary. Of course, we can write our own functions of working with time and watch their behavior, but there is an easier way. 3r3138. 3r3152. 3r3138. 3r3152. If the possible reason for the increase in duration is the "long" interrupt processing, we exclude the possibility of its occurrence in the measurement process. For this, we will wait for its manifestation and only then will we take a measurement cycle. Since an interrupt occurs much less frequently than our measurement cycle lasts, its absence can be guaranteed. We write the corresponding program fragment (using [s] Dirty hacks 3r3-366. Information extracted from the source code) and “this is such street magic”, everything becomes normal - we measure the execution time of 4 and 5 quanta with the average value of the execution time of the addition with the PT in 166 cycles, which corresponds to the previously measured value. The hypothesis can be considered confirmed. 3r3138. 3r3152. 3r3138. 3r3152. One more question remains - what is being done in interrupts for so long, that it takes 3r3138. 3r3152. (7.8) - (5) ~ 2 quantum = * 4 = 8 μs * 16 = 128 processor cycles? We turn to the source code (that is, the assembler code generated by the compiler on godbolt.com) and see that the interrupt itself runs approximately 70 cycles, of which 60 are permanent, and when reading there are additional costs of 10 cycles, totaling 70 when hit on interruption - less than received, but close enough. The difference is attributed to the difference between compilers or modes of their use. 3r3138. 3r3152. 3r3138. 3r3152. Well, now we can measure the actual execution time of the PT addition command with various arguments and make sure that it really changes greatly when the arguments change: from 136 cycles for 0.0 to 190 for ??? (magic number), and it is only 162 for ???. With a probability of 99.9%, this is due to the need for alignment and the peculiarities of its implementation in this particular library, but this study clearly goes beyond the limits of the problem under consideration. 3r3138. 3r3152. 3r3138. 3r3152.
The appendix is the text of the program: r3r383. 3r3384.
3r3386. void setup ()
{
Serial.begin (9600); 3r3152.}
3r3152. volatile float t; //so it is necessary
3r3152. void loop ()
{
int d[170]; 3r3152. unsigned long time, time1; 3r3152. float dt = 1/170 .; 3r3152. 3r3152. for (int i = 0; i <170; ++i) {
{
//wait for the counter to overflow and interrupt processing
time1 = micros ();
long time2;
do {time2 = micros ();} 3r3r215. while (( time2 & ~ 0xFF) == (time1 & ~ 0xFF));
};
/** /
time1 = micros (); //mark the time 3r3152. /*
cli () //here there was an entrance to the critical section - did not help
* /
t = ???; //initial value for the operation 3r3152. t = t + dt; //measured operation 3r3152. /*
sei (); //completion critical sections
* /
time = micros (); //end time
time1 = time-time1;
d[i]= time1 /4;
/*
Serial. print (time1); //here are the results Ali
Serial.flush ();. //not helped remove
emissions Delay (20). //did not help
* /}
;
//derive the stored results, and consider derive average
float sum = 0; 3r3152. for (int i = 0; i <170; ++i) {
sum + = d[i];
Serial.println (d[i]);
};
Serial.println ((sum /170-???) * 4 * 16); //??? - obtained with an empty operation
Serial.flush (); //here we put a breakpoint to see the output graphs 3r3152.}
3r3137. 3r3r138. 3r3152. 3r3148. 3r3148. 3r3148. 3r3152. 3r3152. 3r3145. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r3146. 3r3152. 3r3148. 3r3152. 3r3152. 3r3152. 3r3152.
It may be interesting
weber
Author19-10-2018, 06:23
Publication DateDevelopment / Programming
Category- Comments: 0
- Views: 285
nursing test bank
nursing test bank