English 中文(简体)
使用多线、多线、 pariter 快速超速使用此代码所邀请的想法
原标题:Ideas invited for speeding this code using multi threading ,paritioner

如何将全部元素添加到散列收集程序?

foreach (var x in listx) //List of x
{
    String temp1 = x.sc;
    String temp2 = x.key;
    Nullable<int> temp3 = x.val;

    if ((null != temp2) && (string.Empty != temp2) && (int.MinValue != temp3) && "Fetch" == temp1)
    {
        if (false == htTempVal.ContainsKey(temp2.Trim()))
            htTempVal.Add(temp2.Trim(), temp3);
    }
}
问题回答

只是一些快速的性能改进:

(1) Trim x. Key, 当您在循环中指定临时2 而不是修整临时2 时 。

(2) 是否有可能将htTempVal变成一个哈希塞特?哈希塞特是优化的,允许您在不必担心钥匙是否存在的情况下添加。我看过惊人的性能改进,它压倒了我的物品的GetHashCode,并使用哈希塞特。

这些是小的和容易的, 但如果你有大约100万个记录, 它可能会开始干扰性能。

找到一条安全线的散列桌子吗?

您可以先排序您的项目, 然后只添加唯一的项目吗? 可能存在平行类型 。 您可以跳过 MassesKey () 测试 。

或者,为什么不跳过测试 忽略错误,如果有的话。

最后,一百万件物品怎么会慢下来?必须发生其他事情。

您可以这样做, 以 < code> ConctitutionDigitary 替换散列表格。 它不能保证您会得到任何加速, 因为您基本上是在一个循环中进行原子操作 :

ConcurrentDictionary<String, Nullable<int>> htTempVal = 
           new ConcurrentDictionary<String, Nullable<int>>();

Parallel.ForEach (listx,
  x =>
  {
      String temp1 = x.sc;
      String temp2 = x.key.Trim();
      Nullable<int> temp3 = x.val;

      if ((null != temp2) && (string.Empty != temp2) && 
          (int.MinValue != temp3) && "Fetch" == temp1)
      {
          htTempVal.GetOrAdd(temp2, temp3);
      }
  });

Conctic Ddictionary.GetorAdd 原子检查,如果不存在的话,会添加一个关键值对,或者如果它在字典中已经存在,会返回该值(您可以忽略) 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签