Image noise: Difference between revisions

more improvements! 140 FPS to 185 FPS!
(more improvements! 140 FPS to 185 FPS!)
Line 7:
 
=={{header|C sharp|C#}}==
Max 140185 FPS on .NET 4.0/Windows 7 64-bit on Athlon II X4 620 - ATI Radeon X1200.
 
<lang csharp>using System;
Line 20:
class Program
{
static byte[] colors = { 0, 255 };
static Size size = new Size(320, 240);
static Rectangle rectsize = new Rectangle(new Point(0, 0), size);
Line 32 ⟶ 31:
static double frames = 0;
static Random rand = new Random();
 
static byte tmp;
static byte[] colorswhite = { 0, 255 };
static byte black = 0;
static int halfmax = int.MaxValue / 2; // more voodoo! calling Next() is faster than Next(2)!
 
static IEnumerable<byte> YieldVodoo()
{
// Yield 3 times same number (i.e 255 255 255) for numpixels times.
 
for (int i = 0; i < numpixels; i++)
{
var tmp = colors[rand.Next(2)] < halfmax ? black : white; // no more lists!
 
for// (intno jmore =loops! 0;yield! jyield! < 3; j++)yield!
{yield return tmp;
yield return tmp;
}yield return tmp;
}
}
Line 51 ⟶ 55:
{
// Low-level bitmaps
var bitmap = new Bitmap(size.Width, size.Height);
var data = bitmap.LockBits(rectsize, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
 
Marshal.Copy(
YieldVodoo().ToArray<byte>(),// source
Line 64 ⟶ 68:
}
 
[STAThread]
static void Main()
{
var form = new Form();
 
form.AutoSize = true;
form.Size = new Size(0, 0);
Line 81 ⟶ 86:
worker.DoWork += delegate
{
System.Threading.Thread.Sleep(500); // remove try/catch, just wait a bit before looping
 
while (true)
{
tryvar a = DateTime.Now;
{pb.Image = Randimg();
var ab = DateTime.Now;
pb.Image = Randimg();
var b = DateTime.Now;
 
time += (b - a).TotalSeconds;
frames += 1;
 
if (frames == 30)
{
Console.WriteLine("{0} frames in {1:0.00000} seconds. ({2:0} FPS)", frames, time, frames / time);
 
time = 0;
frames = 0;
}
}
catch
{
// Just pass.
}
}
Line 115:
pb.Size = size;
 
flp.AutoSize = true;
flp.Controls.Add(pb);
 
Anonymous user