Don_Mag icon

QRCode generation test

Don_Mag | PRO | 10/22/20 08:27:45 PM UTC (Edited) | 0 ⭐ | 2987 👁️ | Never ⏰ | []
Swift |

5.4 KB

|

None

|

0 👍

/

0 👎

// QRCode generation test
 
import UIKit
 
class TestViewController: UIViewController {
    
    let imgView: UIImageView = {
        let v = UIImageView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .green
        return v
    }()
    let btn: UIButton = {
        let v = UIButton(type: .system)
        v.translatesAutoresizingMaskIntoConstraints = false
        v.setTitle("Run Test", for: [])
        v.backgroundColor = .yellow
        return v
    }()
    
    var tm1: CFAbsoluteTime!
    var tm2: CFAbsoluteTime!
    var tm3: CFAbsoluteTime!
    var tm4: CFAbsoluteTime!
    
    let strs: [String] = [
        "Simple Text",
        "Longer string here.",
        "This is a much longer string for generating a QR Code image!",
    ]
    
    var idx: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(btn)
        view.addSubview(imgView)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            btn.centerXAnchor.constraint(equalTo: g.centerXAnchor),
            
            imgView.topAnchor.constraint(equalTo: btn.bottomAnchor, constant: 20.0),
            imgView.centerXAnchor.constraint(equalTo: g.centerXAnchor),
            
            imgView.widthAnchor.constraint(equalToConstant: 240.0),
            imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor),
        ])
        
        btn.addTarget(self, action: #selector(self.didTap(_:)), for: .touchUpInside)
        
    }
    
    @objc func didTap(_ sender: Any) {
        
        let str = strs[idx % strs.count]
        
        tm1 = CFAbsoluteTimeGetCurrent()
        
        imgView.generateQRCode(from: str) { image in
            self.tm2 = CFAbsoluteTimeGetCurrent()
            DispatchQueue.main.async {
                self.tm3 = CFAbsoluteTimeGetCurrent()
                self.imgView.image = image;
                self.tm4 = CFAbsoluteTimeGetCurrent()
                self.showResults(str)
            }
        }
        
    }
    
    func showResults(_ s: String) -> Void {
        
        let elapsed1 = tm2 - tm1
        let elapsed2 = tm3 - tm2
        let elapsed3 = tm4 - tm3
        let elapsed4 = tm4 - tm1
        
        let nf = NumberFormatter()
        nf.maximumFractionDigits = 6
        
        idx += 1
        
        print("Generate from string:", s)
        print("elapsed time to generate image:", nf.string(from: NSNumber(value: elapsed1))!)
        print("elapsed time from generate to async execution:", nf.string(from: NSNumber(value: elapsed2))!)
        print("elapsed time to set imageView.image:", nf.string(from: NSNumber(value: elapsed3))!)
        print("overall elapsed time:", nf.string(from: NSNumber(value: elapsed4))!)
        print()
        
    }
    
}
 
// Console output - each test button is tapped 4 times
// on first tap, execution takes much longer than subsequent taps
 
------------------
 
iPhone 7 - Device / 13.3
 
Generate from string: Simple Text
elapsed time to generate image: 0.157093
elapsed time from generate to async execution: 0.000097
elapsed time to set imageView.image: 0.100747
overall elapsed time: 0.257937
 
Generate from string: Longer string here.
elapsed time to generate image: 0.002767
elapsed time from generate to async execution: 0.000066
elapsed time to set imageView.image: 0.003793
overall elapsed time: 0.006626
 
Generate from string: This is a much longer string for generating a QR Code image!
elapsed time to generate image: 0.002132
elapsed time from generate to async execution: 0.000047
elapsed time to set imageView.image: 0.003155
overall elapsed time: 0.005334
 
Generate from string: Simple Text
elapsed time to generate image: 0.005457
elapsed time from generate to async execution: 0.000163
elapsed time to set imageView.image: 0.004744
overall elapsed time: 0.010364
 
------------------
 
iPad Air - Device / 12.1 / 1st gen from 2013
 
Generate from string: Simple Text
elapsed time to generate image: .509212
elapsed time from generate to async execution: .000279
elapsed time to set imageView.image: .321866
overall elapsed time: .831357
 
Generate from string: Longer string here.
elapsed time to generate image: .00809
elapsed time from generate to async execution: .000144
elapsed time to set imageView.image: .009812
overall elapsed time: .018046
 
Generate from string: This is a much longer string for generating a QR Code image!
elapsed time to generate image: .008096
elapsed time from generate to async execution: .000157
elapsed time to set imageView.image: .01149
overall elapsed time: .019743
 
Generate from string: Simple Text
elapsed time to generate image: .010136
elapsed time from generate to async execution: .000077
elapsed time to set imageView.image: .005702
overall elapsed time: .015915
 
------------------
 
iPhone 8 / Simulator / 14.0
 
Generate from string: Simple Text
elapsed time to generate image: 0.133525
elapsed time from generate to async execution: 0.000032
elapsed time to set imageView.image: 0.148432
overall elapsed time: 0.281989
 
Generate from string: Longer string here.
elapsed time to generate image: 0.001354
elapsed time from generate to async execution: 0.000053
elapsed time to set imageView.image: 0.052862
overall elapsed time: 0.054269
 
Generate from string: This is a much longer string for generating a QR Code image!
elapsed time to generate image: 0.002095
elapsed time from generate to async execution: 0.000026
elapsed time to set imageView.image: 0.052522
overall elapsed time: 0.054643
 
Generate from string: Simple Text
elapsed time to generate image: 0.001223
elapsed time from generate to async execution: 0.00005
elapsed time to set imageView.image: 0.052479
overall elapsed time: 0.053752
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎