Created
February 9, 2015 16:48
-
-
Save alphaKAI/c4c57a1a17c357ad2956 to your computer and use it in GitHub Desktop.
昨日の再帰FizzBuzzをマルチスレッドで実行する奴。 fizzbuzzOrderで求めたいfizzbuzzの最後の値を指定し、bitSizeで1スレッドでの計算数を指定できる。
This file contains hidden or 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
| import std.conv, | |
| std.stdio, | |
| std.range, | |
| std.algorithm; | |
| import core.thread; | |
| string[] fizzbuzz(int max, int i = 0, string[] base = null){ | |
| return max == 0 ? base : fizzbuzz(max - 1, ++i, base ~ { | |
| return !(i % 15) ? "FizzBuzz" : | |
| !(i % 3) ? "Fizz" : | |
| !(i % 5) ? "Buzz" : i.to!string; | |
| }()); | |
| } | |
| void main(){ | |
| immutable fizzbuzzOrder = 30000; | |
| immutable bitSize = 50; | |
| immutable threadMax = fizzbuzzOrder / bitSize; | |
| string[fizzbuzzOrder] threadVals; | |
| int threadCount; | |
| foreach(i; 0..(fizzbuzzOrder / bitSize)) | |
| new Thread((){ | |
| threadVals[(bitSize * i)..(bitSize * (i + 1))] = fizzbuzz(bitSize, bitSize * i); | |
| threadCount++;//Add finished thread | |
| }).start; | |
| while(threadCount < (fizzbuzzOrder / bitSize)){}//Wait | |
| foreach(e; threadVals) | |
| e.writeln; | |
| writeln("len:", threadVals.length); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment