You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: issue13/README.md
+217-2Lines changed: 217 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -269,7 +269,40 @@ class HighPriority {
269
269
}
270
270
```
271
271
272
-
TO BE CONTINUED
272
+
I want to now do a test on the traditional `for` loop and run it with the `HighPriority` class which we just wrote, on an iPhone 7 with full optimization on the code, and see how long it takes. But since the code right now includes a `printf()` function, it will take a huge amount of time to execute. That's dumb, so let's remove the `printf()` and make the code smarter so that it won't get optimized out since it's not doing anything, but still do something unnecessarily enough for the code to be executable:
273
+
274
+
```swift
275
+
@inline(never)
276
+
functraditionalForLoop() {
277
+
278
+
var lastValue =0
279
+
for value in0...0xDEADBEEF {
280
+
if value %0xDEED==0 {
281
+
lastValue = value
282
+
}
283
+
}
284
+
if lastValue <0 {
285
+
print(lastValue)
286
+
}
287
+
288
+
}
289
+
```
290
+
291
+
If I run this code with our `HighPriority` class as shown here:
292
+
293
+
```swift
294
+
HighPriority(task: traditionalForLoop).perform { (time) in
@@ -372,22 +405,204 @@ Let's look at the generated assembly code to get some hints as to how this loop
372
405
0x00000001000061bc C0035FD6 ret
373
406
```
374
407
408
+
So it appears as if the call to the `forEach` function of the array was made inline and this code is very much identical to the normal `for` loop.
375
409
410
+
I want to do the same thing with this code and run it through the `HighPriority` class and see how long it takes to execute on an iPhone 7 with full optimization turned on but we have to also make this code smarter so that it doesn't include a `printf()` statement (since that takes a huge amount of time for a loop between `0...0xDEADBEEF`) and also we need to ensure that the code won't just do nothing, since it will get optimized out. So here is the new code:
I don't think any programmer is ever going to write their code like this but as of the time of writing this article, you can create a loop simply by using the `while` statement. I have very few examples where a `while` would be a useful way of doing a loop, especially through numbers, but for the sake of wholesomeness of this article, let's have a look at a `while` loop as well:
The generated assembly code for this `while` loop is very similar to that generated for the `forEach` statement and that on its own very similar to the `for` loop. So as far as the asm code is concerned you can be certain that the compiler is doing a hell of a job! But what about performance. I am going to put this into our `HighPriority` class and run it with full optimization on an iPhone 7:
537
+
538
+
```swift
539
+
HighPriority(task: whileLoop).perform { (time) in
540
+
print(time)
541
+
}
542
+
```
543
+
544
+
And I am receiving the following score: **2.45 seconds**
The reason behind the low score for aesthetics and conciseness are co-related. The `while` statement is physically more syntax to write and hence takes longer to understand than a simple for-loop for a finite task such as going through an array.
384
553
385
554
555
+
`repeat...while` Loop
556
+
===
557
+
This type of loop is very similar to the `while` loop which i talked about earlier with the only difference being that this loop puts the condition at the end of the loop. So you can say that the loop runs at least once and then checks for its condition to exit or continue.
558
+
559
+
```swift
560
+
@inline(never)
561
+
funcrepeatLoop() {
562
+
563
+
var value =0
564
+
var lastValue =0
565
+
566
+
repeat {
567
+
if value %2==0 {
568
+
lastValue = value
569
+
}
570
+
value +=1
571
+
} while value <0xDEADBEEF
572
+
573
+
if lastValue <0 {
574
+
print(lastValue)
575
+
}
576
+
577
+
}
578
+
```
386
579
580
+
I won't go through the ARM assembly code for this type of loop since that's not one of the measurements in our verdicts and I didn't really go into the details of the other ARM assembly outputs either so it's pointless for the purpose of this article. Let's run this code with full optimization with our `HighPriority` class on an iPhone 7 and see the results:
387
581
582
+
```swift
583
+
HighPriority(task: repeatLoop).perform { (time) in
584
+
print(time)
585
+
}
586
+
```
587
+
588
+
And I'm receiving the following value: **2.49 seconds**
I scored the aesthetics and the conciseness categories of the `while` loop lower than the traditional `for` loop since it looks disgusting in my opinion and it also puts the condition of the loop at the end, which makes it less readable. So let's gather all the data:
391
597
392
-
References
598
+
Final Verdict
393
599
===
600
+
601
+
Here are the contestants and their verdicts, **sorted by overal score**:
0 commit comments