WriteableBitmap performance: Debugging faster then Without Debugging

172 views Asked by At

I have this code to measure the performance to draw a single pixel in a WriteableBitmap, and I notice in my system a considerable performance difference between these two starting modes, both in Release configuration.

When starting in Debugging, the speed is ~ 8.6K pixels/second, and when starting without Debugging ~ 2.6K. I was expecting the opposite

I wonder if I'm missing something or what is possible to do to achieve the same performance when starting in Debugging, or it is a bug.

class App1
{
    [STAThread]
    static void Main()
    {
        new Window1().Show();
        new Application().Run();
    }
}

class Image1 : Image
{
    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { }
}

class Window1 : Window
{
    WriteableBitmap writeableBitmap;
    Stopwatch clock1;
    long t1;
    int frames_counter;
    bool frame;
    int px;
    int py;
    Int32Rect single_pixel = new Int32Rect(0, 0, 1, 1);
    byte[] single_pixel_array;
    readonly byte[] red_collor = new byte[] { 0, 0, 255, 0 }; // B G R
    readonly byte[] blue_collor = new byte[] { 255, 0, 0, 0 }; // B G R

    public Window1()
    {
        Height = 150;
        Width = 350;
        Loaded += Window1_Loaded;
    }

    private void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        writeableBitmap = new WriteableBitmap((int)ActualWidth, (int)ActualHeight, 96, 96, PixelFormats.Bgr32, null);

        Content = new Image1
        {
            Stretch = Stretch.None,
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Top,
            Source = writeableBitmap
        };

        single_pixel_array = red_collor;
        frame = true;
        px = 0;
        py = 0;

        var timer2 = new DispatcherTimer(new TimeSpan(0, 0, 0, 1), DispatcherPriority.Background, Callback2, Dispatcher.CurrentDispatcher);
        var timer1 = new DispatcherTimer();
        timer1.Tick += new EventHandler(Callback1);
        timer1.Start();

        clock1 = Stopwatch.StartNew();
        t1 = clock1.ElapsedTicks;
    }

    private void Callback2(object sender, EventArgs e)
    {
        var t2 = clock1.ElapsedTicks;
        var fps = 10000000F / (t2 - t1) * frames_counter;
        Title = $"{fps:F1} fps";
        frames_counter = 0;
        t1 = t2;
    }

    private void Callback1(object sender, EventArgs e)
    {
        writeableBitmap.WritePixels(single_pixel, single_pixel_array, 4, px, py);

        px++;
        if (px > 100)
        {
            px = 0;
            py++;
        }
        if (py > 100)
        {
            py = 0;
            single_pixel_array = frame ? blue_collor : red_collor;
            frame = !frame;
        }
        frames_counter++;
    }
}    enter code here
0

There are 0 answers