Advertisement
Don_Mag

QRCode generation test

Oct 20th, 2020 (edited)
2,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.40 KB | None | 0 0
  1. // QRCode generation test
  2.  
  3. import UIKit
  4.  
  5. class TestViewController: UIViewController {
  6.    
  7.     let imgView: UIImageView = {
  8.         let v = UIImageView()
  9.         v.translatesAutoresizingMaskIntoConstraints = false
  10.         v.backgroundColor = .green
  11.         return v
  12.     }()
  13.     let btn: UIButton = {
  14.         let v = UIButton(type: .system)
  15.         v.translatesAutoresizingMaskIntoConstraints = false
  16.         v.setTitle("Run Test", for: [])
  17.         v.backgroundColor = .yellow
  18.         return v
  19.     }()
  20.    
  21.     var tm1: CFAbsoluteTime!
  22.     var tm2: CFAbsoluteTime!
  23.     var tm3: CFAbsoluteTime!
  24.     var tm4: CFAbsoluteTime!
  25.    
  26.     let strs: [String] = [
  27.         "Simple Text",
  28.         "Longer string here.",
  29.         "This is a much longer string for generating a QR Code image!",
  30.     ]
  31.    
  32.     var idx: Int = 0
  33.    
  34.     override func viewDidLoad() {
  35.         super.viewDidLoad()
  36.        
  37.         view.addSubview(btn)
  38.         view.addSubview(imgView)
  39.        
  40.         let g = view.safeAreaLayoutGuide
  41.         NSLayoutConstraint.activate([
  42.             btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  43.             btn.centerXAnchor.constraint(equalTo: g.centerXAnchor),
  44.            
  45.             imgView.topAnchor.constraint(equalTo: btn.bottomAnchor, constant: 20.0),
  46.             imgView.centerXAnchor.constraint(equalTo: g.centerXAnchor),
  47.            
  48.             imgView.widthAnchor.constraint(equalToConstant: 240.0),
  49.             imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor),
  50.         ])
  51.        
  52.         btn.addTarget(self, action: #selector(self.didTap(_:)), for: .touchUpInside)
  53.        
  54.     }
  55.    
  56.     @objc func didTap(_ sender: Any) {
  57.        
  58.         let str = strs[idx % strs.count]
  59.        
  60.         tm1 = CFAbsoluteTimeGetCurrent()
  61.        
  62.         imgView.generateQRCode(from: str) { image in
  63.             self.tm2 = CFAbsoluteTimeGetCurrent()
  64.             DispatchQueue.main.async {
  65.                 self.tm3 = CFAbsoluteTimeGetCurrent()
  66.                 self.imgView.image = image;
  67.                 self.tm4 = CFAbsoluteTimeGetCurrent()
  68.                 self.showResults(str)
  69.             }
  70.         }
  71.        
  72.     }
  73.    
  74.     func showResults(_ s: String) -> Void {
  75.        
  76.         let elapsed1 = tm2 - tm1
  77.         let elapsed2 = tm3 - tm2
  78.         let elapsed3 = tm4 - tm3
  79.         let elapsed4 = tm4 - tm1
  80.        
  81.         let nf = NumberFormatter()
  82.         nf.maximumFractionDigits = 6
  83.        
  84.         idx += 1
  85.        
  86.         print("Generate from string:", s)
  87.         print("elapsed time to generate image:", nf.string(from: NSNumber(value: elapsed1))!)
  88.         print("elapsed time from generate to async execution:", nf.string(from: NSNumber(value: elapsed2))!)
  89.         print("elapsed time to set imageView.image:", nf.string(from: NSNumber(value: elapsed3))!)
  90.         print("overall elapsed time:", nf.string(from: NSNumber(value: elapsed4))!)
  91.         print()
  92.        
  93.     }
  94.    
  95. }
  96.  
  97. // Console output - each test button is tapped 4 times
  98. // on first tap, execution takes much longer than subsequent taps
  99.  
  100. ------------------
  101.  
  102. iPhone 7 - Device / 13.3
  103.  
  104. Generate from string: Simple Text
  105. elapsed time to generate image: 0.157093
  106. elapsed time from generate to async execution: 0.000097
  107. elapsed time to set imageView.image: 0.100747
  108. overall elapsed time: 0.257937
  109.  
  110. Generate from string: Longer string here.
  111. elapsed time to generate image: 0.002767
  112. elapsed time from generate to async execution: 0.000066
  113. elapsed time to set imageView.image: 0.003793
  114. overall elapsed time: 0.006626
  115.  
  116. Generate from string: This is a much longer string for generating a QR Code image!
  117. elapsed time to generate image: 0.002132
  118. elapsed time from generate to async execution: 0.000047
  119. elapsed time to set imageView.image: 0.003155
  120. overall elapsed time: 0.005334
  121.  
  122. Generate from string: Simple Text
  123. elapsed time to generate image: 0.005457
  124. elapsed time from generate to async execution: 0.000163
  125. elapsed time to set imageView.image: 0.004744
  126. overall elapsed time: 0.010364
  127.  
  128. ------------------
  129.  
  130. iPad Air - Device / 12.1 / 1st gen from 2013
  131.  
  132. Generate from string: Simple Text
  133. elapsed time to generate image: .509212
  134. elapsed time from generate to async execution: .000279
  135. elapsed time to set imageView.image: .321866
  136. overall elapsed time: .831357
  137.  
  138. Generate from string: Longer string here.
  139. elapsed time to generate image: .00809
  140. elapsed time from generate to async execution: .000144
  141. elapsed time to set imageView.image: .009812
  142. overall elapsed time: .018046
  143.  
  144. Generate from string: This is a much longer string for generating a QR Code image!
  145. elapsed time to generate image: .008096
  146. elapsed time from generate to async execution: .000157
  147. elapsed time to set imageView.image: .01149
  148. overall elapsed time: .019743
  149.  
  150. Generate from string: Simple Text
  151. elapsed time to generate image: .010136
  152. elapsed time from generate to async execution: .000077
  153. elapsed time to set imageView.image: .005702
  154. overall elapsed time: .015915
  155.  
  156. ------------------
  157.  
  158. iPhone 8 / Simulator / 14.0
  159.  
  160. Generate from string: Simple Text
  161. elapsed time to generate image: 0.133525
  162. elapsed time from generate to async execution: 0.000032
  163. elapsed time to set imageView.image: 0.148432
  164. overall elapsed time: 0.281989
  165.  
  166. Generate from string: Longer string here.
  167. elapsed time to generate image: 0.001354
  168. elapsed time from generate to async execution: 0.000053
  169. elapsed time to set imageView.image: 0.052862
  170. overall elapsed time: 0.054269
  171.  
  172. Generate from string: This is a much longer string for generating a QR Code image!
  173. elapsed time to generate image: 0.002095
  174. elapsed time from generate to async execution: 0.000026
  175. elapsed time to set imageView.image: 0.052522
  176. overall elapsed time: 0.054643
  177.  
  178. Generate from string: Simple Text
  179. elapsed time to generate image: 0.001223
  180. elapsed time from generate to async execution: 0.00005
  181. elapsed time to set imageView.image: 0.052479
  182. overall elapsed time: 0.053752
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement