枚举和性能 [英] Enum and performance

查看:391
本文介绍了枚举和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有很大不同的查找值,这些值永远不要改变,例如美国各州。而不是把他们变成数据库表,我想用枚举。

My app has a lot of different lookup values, these values don't ever change, e.g. US States. Rather than putting them into database tables, I'd like to use enums.

不过,我不知道做这种方式介入有几个枚举和很多从Int和串,并从我的枚举铸造的。

But, I do realize doing it this way involves having a few enums and a lot of casting from "int" and "string" to and from my enums.

另类,我看到使用字典和LT有人提到;>作为查找表,但执行枚举似乎是清洁

Alternative, I see someone mentioned using a Dictionary<> as a lookup tables, but enum implementation seems to be cleaner.

所以,我想问问,如果保持并通过周围很多枚举和铸造他们成为性能问题或者我应该使用查找表的方法,它执行好?

So, I'd like to ask if keeping and passing around a lot of enums and casting them be a problem to performance or should I use the lookup tables approach, which performs better?

编辑:该铸造需要为ID被存储在其他的数据库表

The casting is needed as ID to be stored in other database tables.

推荐答案

INT 铸造到一个枚举是非常便宜......这将是一个比快字典抬头。基本上这是一个空操作,仅仅复制位转换成位置与不同类型的名义

Casting from int to an enum is extremely cheap... it'll be faster than a dictionary lookup. Basically it's a no-op, just copying the bits into a location with a different notional type.

解析字符串为一个枚举值会略慢。

Parsing a string into an enum value will be somewhat slower.

我怀疑这将是为你,但是你做到这一点,虽然,说实话瓶颈......不知道更多关于你在做什么,这是有点难以建议超出了正常的写最简单的,模式的可读性和可维护性code,将工作,然后检查它执行不够好。

I doubt that this is going to be a bottleneck for you however you do it though, to be honest... without knowing more about what you're doing, it's somewhat hard to recommendation beyond the normal "write the simplest, mode readable and maintainable code which will work, then check that it performs well enough."

这篇关于枚举和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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