[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
<canvas id="drawLineSegmentWithWidthCanvas" width="300" height="200"></canvas> <script> (function () { var canvas = document.getElementById("drawLineSegmentWithWidthCanvas"); var context = canvas.getContext("2d"); context.lineWidth = 10; context.beginPath(); context.moveTo(0, 0); context.lineTo(300, 200); context.stroke(); })(); </script>
<canvas id="drawColoredLineSegmentCanvas" width="300" height="200"></canvas> <script> (function() { var canvas = document.getElementById("drawColoredLineSegmentCanvas"); var context = canvas.getContext("2d"); context.strokeStyle = "rgb(0, 0, 255)"; context.beginPath(); context.moveTo(0, 0); context.lineTo(300, 200); context.stroke(); })(); </script>
<canvas id="drawLineSegmentCanvas" width="300" height="200"></canvas> <script> (function() { var canvas = document.getElementById("drawLineSegmentCanvas"); var context = canvas.getContext("2d"); context.beginPath(); context.moveTo(0, 0); context.lineTo(300, 200); context.stroke(); })(); </script>
<canvas id="drawTriangleCanvas" width="300" height="200"></canvas><br /> <script> (function() { var canvas = document.getElementById("drawTriangleCanvas"); var context = canvas.getContext("2d"); context.beginPath(); context.moveTo(150, 0); context.lineTo(300, 200); context.lineTo(0, 200); context.lineTo(150, 0); context.stroke(); })(); </script>
<canvas id="drawClosedTriangleCanvas" width="300" height="200"></canvas><br /> <script> (function() { var canvas = document.getElementById("drawClosedTriangleCanvas"); var context = canvas.getContext("2d"); context.beginPath(); context.moveTo(150, 0); context.lineTo(300, 200); context.lineTo(0, 200); context.closePath(); context.stroke(); })(); </script>
<canvas id="drawColoredRectangleCanvas" width="300" height="200"></canvas> <script> (function() { var canvas = document.getElementById("drawColoredRectangleCanvas"); var context = canvas.getContext("2d"); context.fillStyle = "rgb(0, 0, 255)"; context.fillRect(20, 40, 200, 100); })(); </script>
<canvas id="drawGradientRectangleCanvas" width="300" height="200"></canvas> <script> (function() { var canvas = document.getElementById("drawGradientRectangleCanvas"); var context = canvas.getContext("2d"); var gradient = context.createLinearGradient(0, 0, 200, 0); gradient.addColorStop(0, "rgb(0, 0, 255)"); gradient.addColorStop(1, "rgb(255, 0, 0)"); context.fillStyle = gradient; context.fillRect(0, 0, 200, 100); })(); </script>
<canvas id="drawPatternRectangleCanvas" width="300" height="200"></canvas> <script> (function() { var canvas = document.getElementById("drawPatternRectangleCanvas"); var context = canvas.getContext("2d"); var image = new Image(); image.src = "https://blog.cnobi.jp/v1/blog/user/118c300f2ee0ad311e5962b0167205f5/1439584363"; image.onload = function () { context.fillStyle = context.createPattern(image, "repeat"); context.fillRect(0, 0, 300, 200); }; })(); </script>
<canvas id="drawRectCanvas" width="300" height="200"></canvas> <script> (function() { var canvas = document.getElementById("drawRectCanvas"); var context = canvas.getContext("2d"); context.fillRect(20, 40, 200, 100); })(); </script>
using System; using System.Linq; using System.Diagnostics; class Program { static void Main(string[] args) { var numbers = Enumerable.Range(0, 100000000).ToArray(); ulong sum = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); foreach (var number in numbers) { sum += (ulong)number; } stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } }実行結果はこうなります。
sum : 4999999950000000 449.1242ms
using System; using System.Linq; using System.Diagnostics; using System.Threading.Tasks; class Program { static void Main(string[] args) { var numbers = Enumerable.Range(0, 100000000).ToArray(); ulong sum = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Parallel.ForEach( numbers, number => { sum += (ulong)number; }); stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } }実行結果はこうなります。
sum : 1852281973816843 1711.2386ms
using System; using System.Linq; using System.Diagnostics; using System.Threading.Tasks; class Program { static void Main(string[] args) { var numbers = Enumerable.Range(0, 100000000).ToArray(); ulong sum = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Parallel.ForEach( numbers, number => { lock (numbers) { sum += (ulong)number; } }); stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } }
sum : 4999999950000000 9717.7726ms
using System; using System.Linq; using System.Diagnostics; using System.Threading.Tasks; class Program { static void Main(string[] args) { var numbers = Enumerable.Range(0, 100000000).ToArray(); ulong sum = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Parallel.ForEach( numbers, () => (ulong)0, (number, state, localSum) => { return localSum + (uint)number; }, threadLocalSum => { lock (numbers) { sum += threadLocalSum; } }); stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } }
sum : 4999999950000000 580.938ms実行時間がさっきよりは短くなりました。
using System; using System.Linq; using System.Diagnostics; using System.Threading.Tasks; class Program { const int iteration = 1000; const int numbers2Count = 10000; static void Main(string[] args) { var numbers = Enumerable.Range(0, iteration); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var sum = serialSum(numbers); //var sum = parallelSum(numbers); stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } private static ulong serialSum(System.Collections.Generic.IEnumerable<int> numbers) { ulong sum = 0; foreach (var number in numbers) { var numbers2 = Enumerable.Range(0, numbers2Count); sum += (ulong)numbers2.Sum(); } return sum; } private static ulong parallelSum(System.Collections.Generic.IEnumerable<int> numbers) { ulong sum = 0; Parallel.ForEach( numbers, () => (ulong)0, (number, state, threadLocalSum) => { return threadLocalSum + (ulong)Enumerable.Range(0, numbers2Count).Sum(); }, threadLocalSum => { lock (numbers) { sum += threadLocalSum; } }); return sum; } }serialSum() : 83.5064ms