性能 - 采用的Guid对象或GUID字符串作为重点 [英] Performance - using Guid object or Guid string as Key

查看:154
本文介绍了性能 - 采用的Guid对象或GUID字符串作为重点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的Guid 为索引的词典,是它更好地使用的Guid 对象,或GUID的字符串表示?

When using a Guid as an index for a Dictionary, is it better to use the Guid object, or the string representation of the Guid?

我只是重构,其使用字符串使用对象的一些代码,因为有新的GUID()呼吁所有的地方。但是,这给我留下了疑惑的性能问题可能是什么。 (该集合是相当小的,但他们得到重复很多次。)

I just refactored some code which was using string to use the object, because there were new Guid() calls all over the place. But that left me wondering what the performance issues might be. (The collections are fairly small, but they get iterated lots of times.)

推荐答案

的Guid 应该是更快,因为比较更简单 - 只是几个直接字节。 。字符串涉及反引用和其它更多的工作。

The Guid should be quicker, as the comparison is simpler - just a few direct bytes. The string involves a dereference and lots more work.

当然 - 你可以配置文件;-p

Of course - you could profile ;-p

证据

Searching for 7f9b349f-f36f-94de-ad96-04279ddf6ecf
As guid: 466; -1018643328
As string: 512; -1018643328
Searching for 870ba465-08f2-c872-cfc9-b3cc1ffa09de
As guid: 470; 1047183104
As string: 589; 1047183104
Searching for d2376f8a-b8c9-4633-ee8e-9679bb30f918
As guid: 423; 1841649088
As string: 493; 1841649088
Searching for 599889e8-d5fd-3618-4c4f-cb620e6f81bb
As guid: 488; -589561792
As string: 493; -589561792
Searching for fb64821e-c541-45f4-0fd6-1c772189dadf
As guid: 450; 1389733504
As string: 511; 1389733504
Searching for 798b9fe5-ba15-2753-357a-7637161ee48a
As guid: 415; 779298176
As string: 504; 779298176
Searching for 12ba292e-8e59-e5d0-7d04-e811a237dc21
As guid: 457; 558250944
As string: 564; 558250944
Searching for 05b3ce14-dfbf-4d3a-1503-ced515decb81
As guid: 413; 1658205056
As string: 504; 1658205056
Searching for 8db4a556-0a65-d8cb-4d0d-0104245d18b8
As guid: 415; 696231936
As string: 506; 696231936
Searching for c49cf80c-5537-fba5-eebd-8ad21bba09c4
As guid: 459; 2100976384
As string: 557; 2100976384

根据:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
static class Program
{

    static void Main()
    {
        Random rand = new Random(123456);
        int COUNT = 1000;
        Dictionary<Guid, int> guids = new Dictionary<Guid, int>(COUNT);
        Dictionary<string, int> strings = new Dictionary<string, int>(
            COUNT, StringComparer.Ordinal);

        byte[] buffer = new byte[16];
        for (int i = 0; i < COUNT; i++)
        {
            rand.NextBytes(buffer);
            Guid guid = new Guid(buffer);
            int val = rand.Next();
            guids.Add(guid, val);
            strings.Add(guid.ToString(), val);
        }

        for(int i = 0 ; i < 10 ; i++) {
            int index = rand.Next(COUNT);
            Guid guid = guids.Keys.Skip(index).First();
            Console.WriteLine("Searching for " + guid);
            int chk = 0;
            const int LOOP = 5000000;
            Stopwatch watch = Stopwatch.StartNew();
            for (int j = 0; j < LOOP; j++)
            {
                chk += guids[guid];
            }
            watch.Stop();
            Console.WriteLine("As guid: " + watch.ElapsedMilliseconds
                   + "; " + chk);
            string key = guid.ToString();
            chk = 0;
            watch = Stopwatch.StartNew();
            for (int j = 0; j < LOOP; j++)
            {
                chk += strings[key];
            }
            watch.Stop();
            Console.WriteLine("As string: " + watch.ElapsedMilliseconds
                   + "; " + chk);
        }
        Console.ReadLine();

    }
}

这篇关于性能 - 采用的Guid对象或GUID字符串作为重点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆