Jim, never do performance in a playground. They’re not optimized for that and more than likely will give you false positives. You really need to do it in a dedicated program, and set it to run a few 100K times. I can help you walk through how to do that if you want.
I have to be honest here the switch solution took some time as i was confused of how to make the tuple matching condition , i like these challenges as they are very good to test basic understanding that we take for granted many times –
Solution with if-else
for i in 1...100
{
if i % 3 == 0 && i % 5 == 0
{
print("FizzBuzz")
}
else if i % 3 == 0
{
print("Fizz")
}
else if i % 5 == 0
{
print("Buzz")
}
else
{
print(i)
}
}
Solution with switch –
var i: Int = 1
while i <= 100
{
let tupleTest = (i % 3, i % 5)
switch tupleTest
{
case (0, 0):
print("FizzBuzz")
case (0, _):
print("Fizz")
case (_, 0):
print("Buzz")
default:
print(i)
}
i += 1
}