Skip to content

Instantly share code, notes, and snippets.

@rxwei
Created September 29, 2019 01:39
Show Gist options
  • Save rxwei/a8a50b9cbb09602f381cc8161f5fad4f to your computer and use it in GitHub Desktop.
Save rxwei/a8a50b9cbb09602f381cc8161f5fad4f to your computer and use it in GitHub Desktop.
Instruction count comparison
@differentiable
func nested_loop(_ x: Float, count: Int) -> Float {
var outer = x
outerLoop: for _ in 1..<count {
outer = outer * x
var inner = outer
var i = 1
while i < count {
inner = inner + x
i += 1
switch Int(inner.truncatingRemainder(dividingBy: 7)) {
case 0: break outerLoop
case 1: break
default: continue
}
}
outer = inner
}
return outer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment