How to Optimize Dynamsoft Barcode Decoding Performance with Parameters
Many enterprises like to use Dynamsoft Barcode Reader SDK due to its flexible parameter configuration and powerful decoding capability for multiple barcodes. In this article, let’s take a glimpse of the barcode SDK template and the possible way to optimize the decoding performance from a developer’s angle.
How to Configure Templates for Decoding Performance
If you have never tried Dynamsoft Barcode Reader SDK, you can have fun with the online barcode playground, simply changing the mode to compare the performance difference straightforwardly.
Furthermore, if you are an expert, you can click advanced settings to adjust a bunch of parameters by yourself.
To facilitate developers, I have uploaded five useful template files to Github:
Decoding speed or decoding accuracy? You can balance the trade-off depends on specific usage scenarios.
Here is my testing image:
Let’s see the detection accuracy and time cost by using the different templates:
BarcodeReader.exe AllSupportedBarcodeTypes.png license.txt speed.jsonTotal barcode(s) found: 12. Time cost: 63 msBarcodeReader.exe AllSupportedBarcodeTypes.png license.txt balanced.jsonTotal barcode(s) found: 13. Time cost: 140 msBarcodeReader.exe AllSupportedBarcodeTypes.png license.txt coverage.jsonTotal barcode(s) found: 13. Time cost: 844 msBarcodeReader.exe AllSupportedBarcodeTypes.png license.txt morecoverage.jsonTotal barcode(s) found: 13. Time cost: 1610 msBarcodeReader.exe AllSupportedBarcodeTypes.png license.txt mostcoverage.jsonTotal barcode(s) found: 13. Time cost: 3156 ms
As you can see, in my case, to guarantee both accuracy and decoding speed, the most suitable template is balance.json.
Is It Possible to Speeding Up Multi-Barcode Decoding Performance with Multi-thread?
In our common sense, the time cost of decoding a single barcode should be less than the time cost of decoding multi-barcodes. So a possible optimization way of reading multiple barcodes is to create multiple worker threads for processing different barcode symbologies simultaneously.
Here is the code for decoding 1D and 2D barcodes sequentially:
barcode_decoding(buffer, size, BF_CODE_39, 1, license, config); barcode_decoding(buffer, size, BF_QR_CODE, 1, license, config); barcode_decoding(buffer, size, BF_PDF417, 1, license, config); barcode_decoding(buffer, size, BF_DATAMATRIX, 1, license, config);
The total time cost is 407ms:
Thread id: 22536. Type: CODE_39Thread id: 22536. Total barcode(s) found: 1. Time cost: 235 msThread id: 22536. Type: QR_CODEThread id: 22536. Total barcode(s) found: 1. Time cost: 47 msThread id: 22536. Type: PDF417Thread id: 22536. Total barcode(s) found: 1. Time cost: 62 msThread id: 22536. Type: DATAMATRIXThread id: 22536. Total barcode(s) found: 1. Time cost: 63 ms
To optimize the decoding performance, I can create four threads to do the same thing:
int starttime = gettime();thread t1(barcode_decoding, buffer, size, BF_CODE_39, 1, license, config);thread t2(barcode_decoding, buffer, size, BF_QR_CODE, 1, license, config);thread t3(barcode_decoding, buffer, size, BF_PDF417, 1, license, config);thread t4(barcode_decoding, buffer, size, BF_DATAMATRIX, 1, license, config);t1.join();t2.join();t3.join();t4.join();int endtime = gettime();printf("Thread time cost: %d ms\n\n", (endtime - starttime));
The final time cost is 265ms:
Thread id: 24024. Type: QR_CODEThread id: 24024. Total barcode(s) found: 1. Time cost: 78 msThread id: 17384. Type: DATAMATRIXThread id: 17384. Total barcode(s) found: 1. Time cost: 78 msThread id: 24264. Type: PDF417Thread id: 24264. Total barcode(s) found: 1. Time cost: 94 msThread id: 4060. Type: CODE_39Thread id: 4060. Total barcode(s) found: 1. Time cost: 265 msThread time cost: 265 ms
So far, it seems good. However, if you pass multiple barcode types to the Dynamsoft barcode decoding API, a magical thing will happen:
barcode_decoding(buffer, size, BF_CODE_39 | BF_DATAMATRIX | BF_QR_CODE | BF_PDF417, 1, license, config);
It is faster than your own multi-thread solution:
Thread id: 20308. Type: PDF417Thread id: 20308. Type: QR_CODEThread id: 20308. Type: DATAMATRIXThread id: 20308. Type: CODE_39Thread id: 20308. Total barcode(s) found: 4. Time cost: 250 ms
The reason is all Dynamsoft barcoding decoding APIs are implemented in threading. Therefore, you do not need to create threads for optimizing decoding performance.
How Thread Count Affects Dynamsoft Barcode SDK Performance?
You may have noticed that there is a parameter named maxAlgorithmThreadCount. Can we boost the SDK performance by increasing the thread count?
I did a simple test based on hardware threads:
Every time I ran the app, I got different results. The performances are not dramatically different by using my testing image:
Thread count: 1. Thread id: 26376. Total barcode(s) found: 13. Time cost: 141 msThread count: 2. Thread id: 26376. Total barcode(s) found: 13. Time cost: 141 msThread count: 3. Thread id: 26376. Total barcode(s) found: 13. Time cost: 125 msThread count: 4. Thread id: 26376. Total barcode(s) found: 13. Time cost: 140 msThread count: 5. Thread id: 26376. Total barcode(s) found: 13. Time cost: 157 msThread count: 6. Thread id: 26376. Total barcode(s) found: 13. Time cost: 203 msThread count: 7. Thread id: 26376. Total barcode(s) found: 13. Time cost: 156 msThread count: 8. Thread id: 26376. Total barcode(s) found: 13. Time cost: 140 msMulti-thread best performance: thread_count = 3, timecost = 125
Apparently, one testing image does not make any sense. Ideally, you should measure the performance with an image dataset. So if you are interested, go and get your hands dirty now.
Source Code
Originally published at https://www.dynamsoft.com on July 22, 2020.