lincolnloop / python-qrcode

Python QR Code image generator
https://pypi.python.org/pypi/qrcode
Other
4.43k stars 676 forks source link

Allow user to skip the best_mask_pattern procedure #126

Closed cryptogun closed 6 years ago

cryptogun commented 7 years ago

Since the low performance described in #124 , it's better to allow user to skip the best_mask_pattern procedure if he/she knows the consequence and is willing to take that risk. My argument is as follows:

  1. Faster. Skip finding best mask pattern can reduce 2/3 of total time generating a qrcode. Test:
    
    import qrcode
    import time

def make(): qr = qrcode.QRCode( version=6, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=4, ) qr.add_data('HELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLDHELLO') qr.make(fit=True)

img = qr.make_image()

start = time.time() for i in range(100): make() end = time.time() print(end-start)


output: 14.734620332717896
And if I comment out [these lines](https://github.com/lincolnloop/python-qrcode/blob/master/qrcode/main.py#L162-L169) to skip the finding of best mask pattern,
output: 5.343664646148682
PS: Using different [penalty rule 1](#125) implementation I wrote.

2. Many other (informal) qrcode generators don't implement any penalty rules at all. And some are super fast.
Like this: [goqr](https://github.com/RaymondChou/goqr), [English fork](https://github.com/loggi/goqr). Written in Go language. The author said that it can *generates 1000 qrcodes in 8s, 8ms/image on everage. Tested under MacBook Pro / i5 2.5G / 4G / Go 1.0.1.* Pretty fast.

3. And if using in Django for generating 50+ qrcodes in one page for a user, CPU will reach 100% during that period(30+ seconds). This is good for DDoS attack.
Best wishes.
SmileyChris commented 7 years ago

Yes, adding the ability to skip best_mask_pattern sounds like a good idea.

cryptogun commented 7 years ago

@SmileyChris Thank you for the reply. I just made a pull request. Check it out :) Still not fast enough, and heavy CPU usage. Maybe I should use large lookup tables. I'll make it in my own forked branch if it is not suitable for everyone use.

hoIIer commented 1 year ago

@SmileyChris @cryptogun I'm running into a similar issue where I am using multiprocessing to generate 10k records, where the qrcode svg generation is a bottleneck and currently much too slow. Is there a way to speed things up? There aren't docs that I saw for the above mark_pattern attribute so unsure how to use it. Thanks!