最大並列スレッドの設定方法
大量の BarCode を読み取る場合、シングルスレッド処理に頼るとパフォーマンスのボトルネックが発生し、スケーラビリティが制限される可能性があります。 しかし、並列スレッドを使用することで、アプリケーションは複数の画像を同時に処理でき、処理能力を効果的に増加させ、バッチジョブの完了にかかる時間を短縮できます。
これらのスレッドに最大制限を設定することは、パフォーマンスを最適化する強力な方法です。 また、プロセッサ・コア間のワークロードをバランスさせることで、アプリケーションがハードウェアの潜在能力をフルに活用できるようにします。 このアプローチは効率を向上させ、アプリケーションをスムーズに動作させながら、より速い結果を提供します。
IronBarcodeはこの制限をコントロールする簡単な方法を提供し、最適なマシンパフォーマンスを実現します。 次のセクションでは、これらのスレッド制限を簡単に設定する方法を示します。
C#で最大並列スレッドを設定する方法
- IronBarcode C#ライブラリをダウンロードし、バーコード読み取りの最大並列スレッドを設定する
- 指定されたフォルダから BarCode を読み込みます。
BarcodeReaderOptionsオブジェクトを新規にインスタンス化します。Multithreadedを true に設定し、MaxParallelThreadsに整数を指定してください。BarcodeResultsで結果を印刷して表示します。
最大並列スレッドの設定
この例では、シングルスレッドではなくマルチスレッドプロセスを使用することのスケーラビリティと効率性を説明するために、大きなバーコード画像のセットを使用します。 画像フォルダをこちらからダウンロードできます。
IronBarcode を複数スレッドで使用するには、まずはじめに新しい BarcodeReaderOptions オブジェクトをインスタンス化し、Multithreaded を true に設定します。 その後、MaxParallelThreads プロパティに整数値を割り当てて設定します。 デフォルトでは、MaxParallelThreads は4に設定されています。
設定を行った後、多数のBarCode画像をフォルダからインポートします。 次に、ループを使用し、Read メソッドを用いてファイルパスと設定された BarcodeReaderOptions を渡してバーコード画像ディレクトリを読み取ります。 最後に、BarcodeResults にアクセスすることでバーコードの値とタイプが表示されます。
using Google.Protobuf.WellKnownTypes;
using IronBarCode;
using System;
using System.IO;
int maxParallelThreads = 4;
var optionsFaster = new BarcodeReaderOptions
{
// Set Max threads to 4
Multithreaded = true,
MaxParallelThreads = maxParallelThreads,
};
// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");
// Retrieve all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");
foreach (var file in pdfFiles)
{
// Read the barcode
var results = BarcodeReader.Read(file, optionsFaster);
foreach (var result in results)
{
// Show the type and value for every barcode found
Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}");
}
}Imports Google.Protobuf.WellKnownTypes
Imports IronBarCode
Imports System
Imports System.IO
Dim maxParallelThreads As Integer = 4
Dim optionsFaster = New BarcodeReaderOptions With {
.Multithreaded = True,
.MaxParallelThreads = maxParallelThreads
}
' Dynamically get the "images" folder in the current directory
Dim folderPath As String = Path.Combine(Directory.GetCurrentDirectory(), "images")
' Retrieve all JPG files in the directory
Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg")
For Each file In pdfFiles
' Read the barcode
Dim results = BarcodeReader.Read(file, optionsFaster)
For Each result In results
' Show the type and value for every barcode found
Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}")
Next
NextImports Google.Protobuf.WellKnownTypes
Imports IronBarCode
Imports System
Imports System.IO
Dim maxParallelThreads As Integer = 4
Dim optionsFaster = New BarcodeReaderOptions With {
.Multithreaded = True,
.MaxParallelThreads = maxParallelThreads
}
' Dynamically get the "images" folder in the current directory
Dim folderPath As String = Path.Combine(Directory.GetCurrentDirectory(), "images")
' Retrieve all JPG files in the directory
Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg")
For Each file In pdfFiles
' Read the barcode
Dim results = BarcodeReader.Read(file, optionsFaster)
For Each result In results
' Show the type and value for every barcode found
Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}")
Next
Next出力

コンソール出力に示すように、対応する各画像の BarCode 値とタイプを表示します。
適切な最大並列スレッドの設定
Multithreaded プロパティが true に設定されている場合、MaxParallelThreads プロパティはデフォルトで4になります。MaxParallelThreads に割り当てる整数の上限はありませんが、ハードウェアの論理コア容量を超える値を設定すると、パフォーマンスが低下する可能性があります。 これは、プロセッサが過度なコンテキスト・スイッチングを処理できず、スピードよりもオーバーヘッドになる可能性があるためです。 したがって、MaxParallelThreads の正しい値はコンピュータの仕様に依存し、開発者は最適な値を見つけるためにテストすべきです。
この例では、上記と同じマルチスレッドのシナリオをタイマーと共に示し、デフォルト値の4とすべての利用可能なスレッドを使用するためのEnvironment.ProcessorCountの比較を行います。 MaxParallelThreads 私たちの場合、32個の論理プロセッサを持つコンピュータを使用しているため、Environment.ProcessorCount を32に設定します。
using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
// Set the max parallel threads to the number of processor cores
int maxParallelThreads = Environment.ProcessorCount;
var optionsFaster = new BarcodeReaderOptions
{
// Set Max threads to the number of processor cores
Multithreaded = true,
MaxParallelThreads = maxParallelThreads,
ExpectMultipleBarcodes = true,
};
// Start timing the process
var stopwatch = Stopwatch.StartNew();
// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");
// Check if directory exists to prevent crashes
if (!Directory.Exists(folderPath))
{
Console.WriteLine($"Error: The directory '{folderPath}' does not exist.");
return;
}
// Get all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");
foreach (var file in pdfFiles)
{
// Read the barcode
var results = BarcodeReader.Read(file, optionsFaster);
if (results.Any())
{
Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
foreach (var result in results)
{
Console.WriteLine($" Value: {result.Value}, Type: {result.BarcodeType}");
}
}
}
stopwatch.Stop();
// Print number of images the barcode reader could decode
Console.WriteLine($" Max parallel threads of {maxParallelThreads} with {stopwatch.Elapsed.TotalSeconds:F2}s");using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
// Set the max parallel threads to the number of processor cores
int maxParallelThreads = Environment.ProcessorCount;
var optionsFaster = new BarcodeReaderOptions
{
// Set Max threads to the number of processor cores
Multithreaded = true,
MaxParallelThreads = maxParallelThreads,
ExpectMultipleBarcodes = true,
};
// Start timing the process
var stopwatch = Stopwatch.StartNew();
// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");
// Check if directory exists to prevent crashes
if (!Directory.Exists(folderPath))
{
Console.WriteLine($"Error: The directory '{folderPath}' does not exist.");
return;
}
// Get all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");
foreach (var file in pdfFiles)
{
// Read the barcode
var results = BarcodeReader.Read(file, optionsFaster);
if (results.Any())
{
Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
foreach (var result in results)
{
Console.WriteLine($" Value: {result.Value}, Type: {result.BarcodeType}");
}
}
}
stopwatch.Stop();
// Print number of images the barcode reader could decode
Console.WriteLine($" Max parallel threads of {maxParallelThreads} with {stopwatch.Elapsed.TotalSeconds:F2}s");Imports IronBarCode
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Linq
' Set the max parallel threads to the number of processor cores
Dim maxParallelThreads As Integer = Environment.ProcessorCount
Dim optionsFaster = New BarcodeReaderOptions With {
.Multithreaded = True,
.MaxParallelThreads = maxParallelThreads,
.ExpectMultipleBarcodes = True
}
' Start timing the process
Dim stopwatch = Stopwatch.StartNew()
' Dynamically get the "images" folder in the current directory
Dim folderPath As String = Path.Combine(Directory.GetCurrentDirectory(), "images")
' Check if directory exists to prevent crashes
If Not Directory.Exists(folderPath) Then
Console.WriteLine($"Error: The directory '{folderPath}' does not exist.")
Return
End If
' Get all JPG files in the directory
Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg")
For Each file In pdfFiles
' Read the barcode
Dim results = BarcodeReader.Read(file, optionsFaster)
If results.Any() Then
Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}")
For Each result In results
Console.WriteLine($" Value: {result.Value}, Type: {result.BarcodeType}")
Next
End If
Next
stopwatch.Stop()
' Print number of images the barcode reader could decode
Console.WriteLine($" Max parallel threads of {maxParallelThreads} with {stopwatch.Elapsed.TotalSeconds:F2}s")Imports IronBarCode
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Linq
' Set the max parallel threads to the number of processor cores
Dim maxParallelThreads As Integer = Environment.ProcessorCount
Dim optionsFaster = New BarcodeReaderOptions With {
.Multithreaded = True,
.MaxParallelThreads = maxParallelThreads,
.ExpectMultipleBarcodes = True
}
' Start timing the process
Dim stopwatch = Stopwatch.StartNew()
' Dynamically get the "images" folder in the current directory
Dim folderPath As String = Path.Combine(Directory.GetCurrentDirectory(), "images")
' Check if directory exists to prevent crashes
If Not Directory.Exists(folderPath) Then
Console.WriteLine($"Error: The directory '{folderPath}' does not exist.")
Return
End If
' Get all JPG files in the directory
Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg")
For Each file In pdfFiles
' Read the barcode
Dim results = BarcodeReader.Read(file, optionsFaster)
If results.Any() Then
Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}")
For Each result In results
Console.WriteLine($" Value: {result.Value}, Type: {result.BarcodeType}")
Next
End If
Next
stopwatch.Stop()
' Print number of images the barcode reader could decode
Console.WriteLine($" Max parallel threads of {maxParallelThreads} with {stopwatch.Elapsed.TotalSeconds:F2}s")出力
4つのスレッドによる処理時間

上記で説明した32コアマシンでこのプロセスを実行した例では、処理時間は約84秒でした。 実際の時間はハードウェアとワークロードによって異なります。
環境 ProcessorCount を用いた処理時間

ご覧の通り、この操作の処理時間はこの例の場合、約53秒で、4スレッドのみで実行した場合よりも速くなっています。 ただし、ホスト・プロセッサに依存するため、より多くのスレッドを使用してもパフォーマンスの向上が保証されるわけではないことに注意してください。 一般的な経験則では、使用可能なプロセッサの最大数から1を引いた数を使用し、他のシステム操作に使用可能なスレッドが1つ残るようにします。
Multithreaded を true に設定し、MaxParallelThreads を増やしてもプロセスの速度は改善されず、実際には低下する可能性があります。よくある質問
最大並列スレッドの設定はどのようにしてバーコード生成のパフォーマンスを最適化しますか?
最大数の並列スレッドを設定することで、すべての利用可能なプロセッサコアを活用し、複数のバーコード画像を同時に処理することができ、処理能力を向上させ、バッチジョブに必要な時間を短縮します。
C#でIronBarcodeを使用して最大並列スレッドをどのように設定しますか?
IronBarcodeで最大並列スレッドを設定するには、Multithreadedをtrueに設定したBarcodeReaderOptionsオブジェクトをインスタンス化し、MaxParallelThreadsに整数値を割り当てます。この設定はバーコード処理を実行するスレッドの数を決定します。
IronBarcodeでの最大並列スレッド数のデフォルトは何ですか?
IronBarcodeのMaxParallelThreadsのデフォルト値は4に設定されています。この初期設定は、ほとんどのマルチコアシステムの効率的な処理を保証することを目的としています。
MaxParallelThreadsの値はプロセッサコア数を超えることができますか?
厳密な制限はありませんが、MaxParallelThreadsを利用可能な論理コアの数を超える値に設定すると、過剰なコンテキストスイッチとオーバーヘッドによって性能が低下する可能性があります。
MaxParallelThreadsの値を設定する際に考慮すべきことは何ですか?
MaxParallelThreadsを設定する際にはハードウェアの論理プロセッサ数を考慮してください。最適な値はプロセッサを圧倒することなく効率的なマルチスレッド化を可能にし、通常は最良のパフォーマンスのためにコア数から1を引いた値を使用します。
MaxParallelThreadsを設定するためにEnvironment.ProcessorCountを使用する効果は何ですか?
Environment.ProcessorCountを使用すると、MaxParallelThreadsは論理プロセッサの総数に設定され、利用可能なスレッドリソースをフルに活用してマルチコアマシンでの処理時間を大幅に改善します。
ハードウェア能力はマルチスレッドバーコード処理にどのような影響を与えますか?
ハードウェアの能力はマルチスレッド処理の効果に直接影響を与えます。論理コアの数はスレッドの実行可能な数を決定し、ワークロードをバランスさせ、リソース制限による性能ボトルネックを避けます。
なぜMaxParallelThreadsを増やすことが常に性能を向上させるとは限らないのですか?
MaxParallelThreadsを増やすと、特にコア数を超える場合、コンテキストスイッチでCPUを圧迫し、効果が薄れ、性能向上ではなく速度低下を引き起こす可能性があります。
例のコードで指定された画像ディレクトリが存在しない場合、何が起こりますか?
指定されたディレクトリが存在しない場合、コードにはエラーメッセージを出力し、プロセスを終了させてバーコード読み取り中のクラッシュを防ぐチェックが含まれています。
最適なMaxParallelThreadsの値を決定するために推奨されるアプローチは何ですか?
最適なMaxParallelThreadsの値を見つけるために、システムのリソースとワークロードの種類を考慮し、特定の環境で異なる設定をテストすることが推奨されます。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。