通用字典表现? [英] Generic Dictionary performance?

查看:59
本文介绍了通用字典表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Group


我正在考虑使用通用词典<>作为一个价值容器内部

我的业务对象,为了跟踪字段的变化或

添加等等。

- 但是在内存使用和性能方面,以极好的

数字(让我们只说100000')实例化/使用Generic Dictionaries有多贵?


有任何实际经验吗?

--------------

简化示例:

公共类SomeItem

{


public SomeItem()

{

}


private Dictionary< string,object> _values = new Dictionary< string,

object>();


private string GetString(string fieldname)

{

if(_values.ContainsKey(fieldname))

返回_values [fieldname];

返回null;

}


private void SetValue(字符串字段名,对象值)

{

if(_values.ContainsKey(fieldname))

_values [fieldname] = value;

else

_values.Add(fieldname,value);

}


公共字符串键

{

get {return this.GetString(" Key"); }

set {this.SetValue(" Key",value); }

}


公共字符串ItemName

{

get {return this.GetString(" ; ITEMNAME"); }

set {this.SetValue(" ItemName",value); }

}


//和更多属性更多

}

Hi Group

I was considering using a Generic Dictionary<> as a value container inside
my business objects, for the reason of keeping track of fields changed or
added and so on.
- But how expensive is it to instantiate/use Generic Dictionaries in great
numbers (let''s just say 100000''s ), in terms of memoryuse and performance?

Any practical experiences out there?
--------------
Simplified example:
public class SomeItem
{

public SomeItem()
{
}

private Dictionary<string, object> _values = new Dictionary<string,
object>();

private string GetString(string fieldname)
{
if(_values.ContainsKey(fieldname))
return _values[fieldname];
return null;
}

private void SetValue(string fieldname, object value)
{
if(_values.ContainsKey(fieldname))
_values[fieldname] = value;
else
_values.Add(fieldname, value);
}

public string Key
{
get { return this.GetString("Key"); }
set { this.SetValue("Key", value); }
}

public string ItemName
{
get { return this.GetString("ItemName"); }
set { this.SetValue("ItemName", value); }
}

// and a number of properties more
}

推荐答案

> - 但就内存使用和性能而言,在伟大的
> - But how expensive is it to instantiate/use Generic Dictionaries in great
数字(让我们只说100000')中实例化/使用Generic Dictionaries有多昂贵?


这里真正的问题不是实例化/使用它有多贵

Generic Dictionaries"但是提供我需要的

功能的最佳解决方案是什么?一个简单的字符串数组可能是解决方案,

,具体取决于您的实际业务需求,或者包括自定义类的任何内容和

。您需要做的是比较应用程序的特定需求

以确定最小的必要功能集,并询问

是否可以提供最小集合的工具功能?那时候,它&b $ b并不重要多么昂贵。它是使用该工具;它唯一重要的是

它是最有效的工具。


-

HTH,


Kevin Spencer

Microsoft MVP

..Net开发人员


假设上帝是只有一个想法 -

想法存在。

因此,上帝存在。


符文B <叶******* @ bingo.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...嗨组

我正在考虑使用通用字典<>作为我的业务对象内部的一个值容器,为了跟踪字段的变化或添加等等。
- 但是实例化/使用Generic Dictionaries有多贵
数字(让我们只说100000'),在记忆使用和表现方面?

有任何实际经验吗?

--- -----------
简化示例:

公共类SomeItem
公共SomeItem()
{
}

私人词典< string,object> _values = new Dictionary< string,
object>();

私有字符串GetString(string fieldname)
{
if(_values.ContainsKey(fieldname))
return _values [fieldname];
返回null;
}
私有void SetValue(字符串字段名,对象值)
{
如果( _values.ContainsKey(fieldname))
_values [fieldname] = value;
else
_values.Add(fieldname,value);
}

公开string Key
{get / return this.GetString(" Key"); }
设置{this.SetValue(" Key",value); }

公共字符串ItemName
{
get {return this.GetString(" ItemName"); }
设置{this.SetValue(" ItemName",value); }

//和许多属性更多
}
numbers (let''s just say 100000''s ), in terms of memoryuse and performance?
The real question here is not "how expensive is it to instantiate/use
Generic Dictionaries" but "what is the most optimal solution to provide the
functionality I require? A simple string array might be the solution,
depending on your actual business requirements, or anything up to and
including a custom class. What you have to do is compare the specific needs
of your app to determine a minimum set of necessary features, and ask what
is the tool that can provide that minimum set of features? At that point, it
doesn''t matter "how expensive" it is to use that tool; it only matters that
it is the most efficient tool for the job.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Rune B" <ye*******@bingo.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl... Hi Group

I was considering using a Generic Dictionary<> as a value container inside
my business objects, for the reason of keeping track of fields changed or
added and so on.
- But how expensive is it to instantiate/use Generic Dictionaries in great
numbers (let''s just say 100000''s ), in terms of memoryuse and performance?

Any practical experiences out there?
--------------
Simplified example:
public class SomeItem
{

public SomeItem()
{
}

private Dictionary<string, object> _values = new Dictionary<string,
object>();

private string GetString(string fieldname)
{
if(_values.ContainsKey(fieldname))
return _values[fieldname];
return null;
}

private void SetValue(string fieldname, object value)
{
if(_values.ContainsKey(fieldname))
_values[fieldname] = value;
else
_values.Add(fieldname, value);
}

public string Key
{
get { return this.GetString("Key"); }
set { this.SetValue("Key", value); }
}

public string ItemName
{
get { return this.GetString("ItemName"); }
set { this.SetValue("ItemName", value); }
}

// and a number of properties more
}





" Kevin Spencer" <柯*** @ DIESPAMMERSDIEtakempis.com>在消息中写道

news:OU ************** @ TK2MSFTNGP10.phx.gbl ...

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:OU**************@TK2MSFTNGP10.phx.gbl...
- 但是在内存使用和性能方面,实例化/使用Generic Dictionaries的数量是多少(让我们只说100000')?
- But how expensive is it to instantiate/use Generic Dictionaries in
great numbers (let''s just say 100000''s ), in terms of memoryuse and
performance?



这里真正的问题不是实例化/使用
Generic Dictionaries有多贵?但是提供我需要的功能的最佳解决方案是什么?一个简单的字符串数组可能是解决方案,
取决于您的实际业务需求,或任何包括自定义类的任何内容。您需要做的是比较应用程序的特定需求以确定最小的必要功能集,并询问可以提供最少功能集的工具是什么?在那一点上,多么昂贵并不重要。它是使用该工具;它只是它是最有效的工具。



The real question here is not "how expensive is it to instantiate/use
Generic Dictionaries" but "what is the most optimal solution to provide
the functionality I require? A simple string array might be the solution,
depending on your actual business requirements, or anything up to and
including a custom class. What you have to do is compare the specific
needs of your app to determine a minimum set of necessary features, and
ask what is the tool that can provide that minimum set of features? At
that point, it doesn''t matter "how expensive" it is to use that tool; it
only matters that it is the most efficient tool for the job.




是的,我明白了你的观点 - 如果是一个字典< string,object>的功能;

正是这里所需要的。 (有点类似于DataRow没有

版本和架构)。


当我用
$查看字典时我只关心性能b $ b反射器。它乍一看似乎是一个相当复杂的结构,有很多拳击和内部的东西正在进行......所以我考虑过解决它

更简单模型,这将需要更多的代码在项目旁边



- 但为什么打扰它,如果它实际上表现得非常好......


R



Yeah, I see your point - the functionality if an Dictionary<string, object>
is exactly what is needed here. (somewhat similar to a DataRow without
versions and schema).

I just got concerned with performance when I looked at the Dictionary with
reflector. It seemed at first glance like an pretty complex structure, with
a lot of boxing and internal stuff going on... so I''ve considered solving it
with a simpler model, that would require a lot more code out side the "item"
class
- but why bother if it actually performed very well...

R


Kevin Spencer< ke *** @ DIESPAMMERSDIEtakempis.com>写道:
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
- 但是以极好的数字来实例化/使用通用词典是多么昂贵(让我们只说100000'),在内存使用和性能方面?
- But how expensive is it to instantiate/use Generic Dictionaries in great
numbers (let''s just say 100000''s ), in terms of memoryuse and performance?



这里真正的问题不是实例化/使用
Generic Dictionaries有多贵。但是提供我需要的功能的最佳解决方案是什么?



The real question here is not "how expensive is it to instantiate/use
Generic Dictionaries" but "what is the most optimal solution to provide the
functionality I require?




我会说它甚至都没有。我会问:什么是最简单,最可靠的b $ b可读和可维护的解决方案,它为我提供了性能和我需要的b $ b功能?


最优解决方案通常远非最简单的解决方案,但是

通常最简单的一个表现得足够*。


当然,这篇文章在解释时才有意义最佳

就性能而言 - 如果你的意思更全面,那么b b b b b b b b b b b b b b b b b b b b感觉,那么我们可能已经同意了:)


-

Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeettarget =_ blank> http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该组,请不要给我发邮件



I would say it''s not even that. I''d ask: "What is the simplest, most
readable and maintainable solution which gives me the performance and
functionality I require?"

The "most optimal" solution is often far from the simplest one, but
often the simplest one performs well *enough*.

Of course, this post only makes sense when interpreting "most optimal"
in terms of performance - if you meant it in a more holistic "best in
many different ways" sense, then we probably already agree :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于通用字典表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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