I have been doing programming for 1C for several years, and then the thought
came to me -
“But can you take some kind of training course, all of a sudden there are some gaps in knowledge that I hadn’t even suspected before”? No sooner said than done. I sit, listen to the course, reach the cyclic operators, and then a second thought
(yes, they do not often appear) -
“Which cycle is faster”? It would be necessary to check.
So, I found
five ways to organize a cycle using 1C tools.
The first type of cycle, let's call it conditionally
"For Po" looks like this:
= 0 (); ;
The second kind of
"For Everyone" :
(); ;
The third
"Bye" :
')
<> (); = + 1; ;
Then I remembered assembly youth - the
“If” cycle:
~: <> (); = + 1; ~; ;
And finally,
"Recursion" (, ) (); <> (+1, ); ;
Naturally, it is not entirely correct to attribute recursion to cycles, but nevertheless with its help one can achieve similar results. At once I will make a reservation that the recursion did not participate in further testing. Firstly, all the tests were performed at 1,000,000 iterations, and the recursion falls already at 2,000. Secondly, the recursion rate is ten times less than the speed of the remaining cycles.
Last retreat. One of the conditions was to perform any actions in the cycle. First, an empty cycle is used very rarely. Secondly, the “For Everyone” cycle is used for a collection, and therefore the other cycles must work with the collection in order for testing to take place under the same conditions.
Well, let's go. A reading from a pre-filled array was used as the loop body.
= .();
or, using the “For Each” cycle
= ;
Testing was conducted on platform 8.3.5.1231 for three types of interface (Regular Application, Managed Application and Taxi).
Results for 8.3.5.1231Interface | For by | For each | Until | If a |
Normal application | 5734.2 | 4680.4 | 7235.4 | 7263.0 |
Managed Application | 5962.4 | 4882.6 | 7497.4 | 7553.6 |
Taxi | 5937.2 | 4854.6 | 7500,8 | 7513.0 |
Numbers is the time in milliseconds obtained using the function
CurrentUniversalDataVMilliseconds () , which I called before the loop and after its completion. The numbers are fractional, because I used the arithmetic average of five measurements. Why haven't I used Performance Measurement? I had no goal to measure the speed of each line of code, only the speed of cycles with the same result.
It would seem that everything, but - test it so test it!
Result for the platform 8.2.19.106
Results for 8.2.19.106Interface | For by | For each | Until | If a |
Normal application | 4411.8 | 3497.2 | 5432.0 | 5454.0 |
Managed Application | 4470.8 | 3584.8 | 5522,6 | 5541.0 |
On average, platform 8.2 is 25% faster than 8.3. I didn’t expect such a difference a bit and decided to test it on another machine. I will not give the results, you can upgrade them yourself using
this configuration. I can only say that there 8.2 was faster by 20 percent.
Why? I don’t know, I didn’t plan to disassemble the core in my plans, but I still looked at the performance measurement. It turned out that the cyclic operations themselves in 8.3 are somewhat faster than in 8.2. But on line
= .();
that is, when reading a collection item into a variable, there is a significant decrease in performance.
Eventually:
The percentage of the speed of cyclesFor by | For each | Until | If a |
78.97% | 64.57% | 99.57% | 100.00% |
Why all this? For myself, I made several conclusions:
1. If there is an opportunity to use a specialized cycle - “For Everyone”, then it is better to use it. By the way, by itself it works longer than other cycles, but the speed of access to the collection item is much higher.
2. If you know in advance the number of iterations - use "For Po". "Bye" will work slower.
3. If you use the cycle "If" - other programmers obviously will not understand you.