I've written a render function that is called every 1/60 of a second. It's shown below. Essentially, it renders 40 lines in radial pattern that uses sine. It looks like this https://i.sstatic.net/HPwOd.jpg. When I run it on my iPhone 6s with the debugger on, it shows that it is using around 60% of the CPU and it just makes the "high" battery usage level. This doesn't seem right to me, it's only 40 simple lines right?
Could I be doing this more efficiently? Should I try to use something else like CALayer or Metal for better performance? I'm a noob at this by the way.
func render() { tick += 0.01 let renderer = UIGraphicsImageRenderer(size: CGSize(width: 375, height: 667)) let img = renderer.image { ctx in let c = ctx.cgContext // Set background let background = CGRect(x: 0, y: 0, width: 375, height: 667) c.setFillColor(UIColor.black.cgColor) c.addRect(background) c.drawPath(using: .fillStroke) // Function to draw a line func line(p1:CGPoint, p2:CGPoint) { let line = CGRect(x: 0, y: 0, width: 200, height: 200) c.addLines(between: [p1,p2]) c.drawPath(using: .fillStroke) } // Draw lines c.setStrokeColor(UIColor.white.cgColor) c.setLineWidth(1) c.setLineCap(CGLineCap.round) for i in 0...39 { let n:CGFloat = CGFloat(i) * CGFloat.pi/40 let d:CGFloat = sin(tick + CGFloat(i)*CGFloat.pi/5)*20 + 100 line(p1: CGPoint(x: d*cos(tick+n)+187.5, y: d*sin(tick+n)+333.5), p2: CGPoint(x: d*cos(tick+n+CGFloat.pi)+187.5, y: d*sin(tick+n+CGFloat.pi)+333.5)) } } imageView.image = img }
while true { print("loop" }should be fast, it's only 1 line, right?cos2400 times per second andsin3600 times per second as well as performing at least 16800 multiplications per second all just inside theforloop.