我已经完成了我的作业,并且一再保证,无论是在for循环内部还是外部声明变量,它在性能上都没有区别,它实际上编译为完全相同的MSIL.但是我一直在弄乱它,并发现在循环中移动变量声明确实会导致相当大的一致性能增益.
我编写了一个小型控制台测试类来测量这种效果.我初始化一个静态double[]
数组项,两个方法对它执行循环操作,将结果写入静态double[]
数组缓冲区.最初,我的方法是那些我注意到差异的方法,即复数的大小计算.对于长度为1000000 的项目数组运行这些100次,我得到的变量(6个double
变量)在循环内的运行时间一直较低:例如,32,83±0,64 ms v 43,44±使用Intel Core 2 Duo @ 2.66 GHz的老式配置为0.45 ms.我尝试以不同的顺序执行它们,但它没有影响结果.
然后我意识到计算复数的大小远非最小的工作示例,并测试了两个更简单的方法:
static void Square1()
{
double x;
for (int i = 0; i < buffer.Length; i++) {
x = items[i];
buffer[i] = x * x;
}
}
static void Square2()
{
for (int i = 0; i < buffer.Length; i++) {
double x;
x = items[i];
buffer[i] = x * x;
}
}
Run Code Online (Sandbox Code Playgroud)
有了这些,结果就出现了另一种方式:在循环外声明变量似乎更有利:对于Square1() …
我有一个double[][]
可能被多个线程同时修改的锯齿状数组。我想让它成为线程安全的,但如果可能的话,不要锁。线程很可能以数组中的相同元素为目标,这就是整个问题出现的原因。我找到了使用该Interlocked.CompareExchange
方法原子地增加双精度值的代码:为什么没有接受双精度作为参数的 Interlocked.Add 的重载?
我的问题是:如果 中有锯齿状的数组引用,它会保持原子性Interlocked.CompareExchange
吗?非常感谢您的见解。
举个例子:
public class Example
{
double[][] items;
public void AddToItem(int i, int j, double addendum)
{
double newCurrentValue = items[i][j];
double currentValue;
double newValue;
SpinWait spin = new SpinWait();
while (true) {
currentValue = newCurrentValue;
newValue = currentValue + addendum;
// This is the step of which I am uncertain:
newCurrentValue = Interlocked.CompareExchange(ref items[i][j], newValue, currentValue);
if (newCurrentValue == currentValue) break;
spin.SpinOnce();
}
}
}
Run Code Online (Sandbox Code Playgroud) 在我的程序中,我有一个ObservableKeyedCollection<TKey, TItem>
继承自KeyedCollection<TKey, TItem>
并实现的抽象类INotifyCollectionChanged
.
这个抽象类的实现必然是一个ListBox
.在这里ListBox
,我在双击时编辑项目,一旦接受,我从该ObservableKeyedCollection<TKey, TItem>
实现中删除已编辑项目的旧实例,并添加已修改的新实例.
在Windows 10 Creators Update(1703,内部版本号15063.250)之前,这一切都运行良好.自更新以来,ObservableKeyedCollection<TKey, TItem>
开始InvalidOperationException
使用以下消息抛出s:
调用线程无法访问此对象,因为另一个线程拥有它.
我不在代码的这个区域中使用任何异步操作.
整个堆栈跟踪太长,但这里是开头的顶部OnCollectionChanged
:
在System.Windows.Threading.Dispatcher.VerifyAccess()在System.Windows.Threading.DispatcherObject.VerifyAccess()在System.Windows.DependencyObject.GetValue(的DependencyProperty DP)在System.Windows.Controls.Primitives.Selector.GetIsSelected(DependencyObject的元件)在System.Windows.Controls.Primitives.Selector.ItemSetIsSelected(ItemInfo信息,布尔值)System.Windows.Controls.Primitives.Selector.SelectionChanger.CreateDeltaSelectionChange(List'1 unselectedItems,List'1 selectedItems)在System.Windows .Controls.Primitives.Selector.SelectionChanger.End()在System.Windows.Controls.Primitives.Selector.RemoveFromSelection(NotifyCollectionChangedEventArgs e)上System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)上System.Windows.Controls .ItemsControl.OnItemCollectionChanged2(对象发件人,NotifyCollectionChangedEventArgs e)上System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(对象发件人,NotifyCollectionChang edEventArgs e)位于System.Windows.WeakE.EventCollection.OnCollectionChanged(Object sender,NotifyCollectionChangedEventArgs e)的System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args),位于System.Windows.WeakEventManager.ListenerList'1.DeliverEvent(Object sender, EventArgs的,类型managerType)在System.Windows.WeakEventManager.DeliverEventToList(对象发件人,EventArgs指定参数时,ListenerList列表)在System.Windows.WeakEventManager.DeliverEvent(对象发件人,EventArgs参数)在System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(对象发件人,NotifyCollectionChangedEventArgs参数)在System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs参数)在System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs指定参数时,的Int32 adjustedOldIndex,的Int32 adjustedNewIndex)在System.Windows.Data.ListCollectionView.ProcessCollectionChanged (通知 CollectionChangedEventArgs args)位于E:\ Phil \Programozás\ Modulok\TetheredSun.1.0\TetheredSun\ObservableKeyedCollection的TetheredSun.ObservableKeyedCollection'2.OnCollectionChanged(NotifyCollectionChangedEventArgs e)的System.Windows.Data.CollectionView.OnCollectionChanged(Object sender,NotifyCollectionChangedEventArgs args). cs,行号:68 at TetheredSun.ObservableKeyedCollection`2.RemoveItem(Int32 index)at [...]
编辑1:
以下是在创建者更新(覆盖KeyedCollection<TKey, TItem>.RemoveItem(int index)
)之前可以正常工作的违规代码部分:
protected override void RemoveItem(int index)
{
TItem item = this[index];
base.RemoveItem(index); …
Run Code Online (Sandbox Code Playgroud) 我尝试使用计算列表的平均值Parallel.For()
.我决定反对它,因为它比简单的串行版本慢大约四倍.然而我很感兴趣的是它并没有产生与序列结果完全相同的结果,我认为了解原因是有益的.
我的代码是:
public static double Mean(this IList<double> list)
{
double sum = 0.0;
Parallel.For(0, list.Count, i => {
double initialSum;
double incrementedSum;
SpinWait spinWait = new SpinWait();
// Try incrementing the sum until the loop finds the initial sum unchanged so that it can safely replace it with the incremented one.
while (true) {
initialSum = sum;
incrementedSum = initialSum + list[i];
if (initialSum == Interlocked.CompareExchange(ref sum, incrementedSum, initialSum)) break;
spinWait.SpinOnce();
}
});
return sum / list.Count;
} …
Run Code Online (Sandbox Code Playgroud)