下面是Conway's Game of Life在WPF中的一个(非常幼稚的)实现。这只是个演示..。
xaml:
<Window x:Class="wpf_conway_life_2013_05_19.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="500">
<Grid>
<Canvas Name="canvas"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
</Canvas>
</Grid>
</Window>代码隐藏:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace wpf_conway_life_2013_05_19
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var random = new Random();
var data = new int[100, 100];
var dataB = new int[100, 100];
Func<int, int, int> at = (x, y) =>
{
if (x < 0) x = 100 + x;
if (x >= 100) x = x % 100;
if (y < 0) y = 100 + y;
if (y >= 100) y = y % 100;
return data[x, y];
};
for (var x = 0; x < 100; x++)
for (var y = 0; y < 100; y++)
data[x, y] = random.Next(2);
var rectangles = new Rectangle[100, 100];
for (var x = 0; x < 100; x++)
for (var y = 0; y < 100; y++)
{
rectangles[x, y] = new Rectangle();
canvas.Children.Add(rectangles[x, y]);
}
canvas.SizeChanged += (s, e) =>
{
for (var x = 0; x < 100; x++)
{
for (var y = 0; y < 100; y++)
{
rectangles[x, y].Width = canvas.ActualWidth / 100;
rectangles[x, y].Height = canvas.ActualHeight / 100;
Canvas.SetLeft(rectangles[x, y], (canvas.ActualWidth / 100) * x);
Canvas.SetTop(rectangles[x, y], (canvas.ActualHeight / 100) * y);
}
}
};
Action macroStep = () =>
{
dataB = new int[100, 100];
for (var x = 0; x < 100; x++)
{
for (var y = 0; y < 100; y++)
{
var neighbors = 0;
for (var i = -1; i <= 1; i++)
for (var j = -1; j <= 1; j++)
if (i == 0 && j == 0)
continue;
else
neighbors += at(x + i, y + j);
dataB[x, y] = data[x, y];
if (neighbors < 2) dataB[x, y] = 0;
if (neighbors == 3) dataB[x, y] = 1;
if (neighbors > 3) dataB[x, y] = 0;
rectangles[x, y].Fill = dataB[x, y] == 0 ? new SolidColorBrush(new Color()) : Brushes.Black;
}
}
data = dataB;
};
var timer = new DispatcherTimer();
timer.Tick += (s, e) => macroStep();
timer.Start();
}
}
}它看起来是这样的:

如果我用Brushes.White代替new SolidColorBrush(new Color()),程序的运行速度会慢得多。为什么?
我正在使用2010 Express在64位Windows 7上进行测试。
发布于 2013-05-21 03:13:16
因为new Color()的alpha值为零,这意味着WPF不需要渲染它,因为它是完全透明的-另一方面,白色的alpha值是255,这意味着它是必须渲染的完全纯白的颜色。
发布于 2013-06-04 10:49:24
使用Brushes.White没有什么特别之处。
如果在macroStep事件处理程序外部定义自己的本地笔刷,然后将其冻结,则它的行为将与使用Brushes.White完全相同。如果你不先冻结它,它会表现得非常糟糕。
最佳性能是在每次开始调用macroStep时,在循环之前创建一次笔刷,然后冻结它。如果你在最里面的循环中创建一个新的笔刷,它会显着减慢。
此外,如果您为行为不佳的代码增加计时器的时间间隔,它实际上会修复性能问题。我的猜测是,在后台线程每次完成渲染后,都会发生某种类型的资源清理,这与画笔的内部结构有关,但它无法进行清理,因为您将在下一次迭代中转过身来使用画笔。为了演示这一点,我创建了一个笔刷池,每次都使用不同的笔刷:
SolidColorBrush[] brushes = new SolidColorBrush[2];
for (int i = 0; i < brushes.Length; i++)
{
var brush = new SolidColorBrush(new Color());
brush.Freeze();
brushes[i] = brush;
}
int brushIx = 0;
Action macroStep = () =>
{
dataB = new int[100, 100];
var brush = brushes[brushIx++ % brushes.Length];
...
rectangles[x, y].Fill = dataB[x, y] == 0
? brush
: Brushes.Black;
data = dataB;
};如果将笔刷数量设置为1,这将提供与使用Brushes.White相同的行为。但是,如果您将其设置为2或更多,您将获得预期的性能。
https://stackoverflow.com/questions/16642461
复制相似问题