Created
March 26, 2017 00:17
-
-
Save muropclabadmin/79540ba0c9ea6de6ff26fdb57e841293 to your computer and use it in GitHub Desktop.
C#でParallelを使った画像読み込みのサンプルプログラム。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Diagnostics; | |
namespace SamplePleject | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
SingleTask(); | |
MaltiTask(); | |
Console.ReadLine(); | |
} | |
public static void SingleTask() | |
{ | |
Console.WriteLine($"SingleTask Start."); | |
var path = Path.GetFullPath(@".\img"); | |
var files = Directory.EnumerateFiles(path); | |
List<Bitmap> list = new List<Bitmap>(files.Count()); | |
var stopWatch = Stopwatch.StartNew(); | |
foreach (var x in files) | |
{ | |
using (var fs = new FileStream(x, FileMode.Open, FileAccess.Read, FileShare.Read)) | |
{ | |
var sw = Stopwatch.StartNew(); | |
Bitmap b = new Bitmap(fs); | |
list.Add(b); | |
Console.WriteLine($"{sw.ElapsedMilliseconds}ms: {new FileInfo(x).Name}"); | |
} | |
} | |
Console.WriteLine($"{stopWatch.ElapsedMilliseconds}ms"); | |
list.Clear(); | |
Console.WriteLine($"SingleTask End."); | |
} | |
public static void MaltiTask() | |
{ | |
Console.WriteLine($"MaltiTask Start."); | |
var path = Path.GetFullPath(@".\img"); | |
var files = Directory.EnumerateFiles(path); | |
List<Bitmap> list = new List<Bitmap>(files.Count()); | |
var stopWatch = Stopwatch.StartNew(); | |
Parallel.ForEach(files, x => | |
{ | |
using (var fs = new FileStream(x, FileMode.Open, FileAccess.Read, FileShare.Read)) | |
{ | |
var sw = Stopwatch.StartNew(); | |
Bitmap b = new Bitmap(fs); | |
list.Add(b); | |
Console.WriteLine($"{sw.ElapsedMilliseconds}ms: {new FileInfo(x).Name}"); | |
} | |
}); | |
Console.WriteLine($"end. {stopWatch.ElapsedMilliseconds}ms"); | |
list.Clear(); | |
Console.WriteLine($"MaltiTask End."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment