Skip to main content
removed unnecessary modulo
Source Link
PTwr
  • 526
  • 4
  • 6
        readonly static int white = Color.White.ToArgb();
        readonly static int black = Color.Black.ToArgb();
        readonly static Random NoiseRnd = new Random();
        private static DirectBitmap GetStaticNoise(int width, int height)
        {
            var bitmap = new DirectBitmap(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    var rnd = NoiseRnd.Next(0, 2);

                    bitmap.SetPixel(x, y, rnd % 2 == 0 ? white : black);
                }
            }

            return bitmap;
        }
        readonly static int white = Color.White.ToArgb();
        readonly static int black = Color.Black.ToArgb();
        readonly static Random NoiseRnd = new Random();
        private static DirectBitmap GetStaticNoise(int width, int height)
        {
            var bitmap = new DirectBitmap(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    var rnd = NoiseRnd.Next(0, 2);

                    bitmap.SetPixel(x, y, rnd % 2 == 0 ? white : black);
                }
            }

            return bitmap;
        }
        readonly static int white = Color.White.ToArgb();
        readonly static int black = Color.Black.ToArgb();
        readonly static Random NoiseRnd = new Random();
        private static DirectBitmap GetStaticNoise(int width, int height)
        {
            var bitmap = new DirectBitmap(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    var rnd = NoiseRnd.Next(0, 2);

                    bitmap.SetPixel(x, y, rnd == 0 ? white : black);
                }
            }

            return bitmap;
        }
micro optimizations are hard
Source Link
PTwr
  • 526
  • 4
  • 6

However, if your loop is tight enough and dataset fits into cpu cache, results will become Damn you CPU loop unrolling!

In the end, the real winner is white*0, but... that won't work in opaque ARGB colors, where A is 255.

TLDR: microoptimizations are hard and CPU doing its own tricks makes it even harder :)

In the end, the real winner is white*0, but... that won't work in opaque ARGB colors, where A is 255.

However, if your loop is tight enough and dataset fits into cpu cache, results will become Damn you CPU loop unrolling!

In the end, the real winner is white*0, but... that won't work in opaque ARGB colors, where A is 255.

TLDR: microoptimizations are hard and CPU doing its own tricks makes it even harder :)

MORE SPEED!
Source Link
PTwr
  • 526
  • 4
  • 6

To clarify on using Ternary Operator as performance optimization: Two-element array is twice as fast as full If statement, and Ternary Operator is ~30% faster than array. Original code used modulo 2 to check parity, but with Random already returning only 0 or 1 it can be used without modulo for small performance gain. Ternary is coolconditional assignments comparison

But if you reorder IF statements slightly, you can squeeze out a bit more performance: reordered IF's Have in mind that (ab)using array indexes as logic (pointers) should work better in native code, without all the fancy memory management of .NET. Or it might not, depending on performance tricks done by CPU's themselves. Micro optimizations are tricky.

In the end, the real winner is white*0, but... that won't work in opaque ARGB colors, where A is 255.

To clarify on using Ternary Operator as performance optimization: Two-element array is twice as fast as full If statement, and Ternary Operator is ~30% faster than array. Original code used modulo 2 to check parity, but with Random already returning only 0 or 1 it can be used without modulo for small performance gain. Ternary is cool Have in mind that (ab)using array indexes as logic (pointers) should work better in native code, without all the fancy memory management of .NET. Or it might not, depending on performance tricks done by CPU's themselves. Micro optimizations are tricky.

To clarify on using Ternary Operator as performance optimization: Two-element array is twice as fast as full If statement, and Ternary Operator is ~30% faster than array. Original code used modulo 2 to check parity, but with Random already returning only 0 or 1 it can be used without modulo for small performance gain. conditional assignments comparison

But if you reorder IF statements slightly, you can squeeze out a bit more performance: reordered IF's Have in mind that (ab)using array indexes as logic (pointers) should work better in native code, without all the fancy memory management of .NET. Or it might not, depending on performance tricks done by CPU's themselves. Micro optimizations are tricky.

In the end, the real winner is white*0, but... that won't work in opaque ARGB colors, where A is 255.

Ternary Operator vs Array indexing
Source Link
PTwr
  • 526
  • 4
  • 6
Loading
Source Link
PTwr
  • 526
  • 4
  • 6
Loading