替代可空类型在C# [英] Alternatives to nullable types in C#

查看:176
本文介绍了替代可空类型在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的一系列数字数据,其中有时,该系列的值必须是空的工作算法。但是,因为这个应用程序的性能很关键,我都避免使用可空类型。我有PERF测试的算法,专门比较使用可空类型与非可空类型的业绩,并在最好的情况下可空类型的2倍速度较慢,但​​往往差得很远。



最经常使用的数据类型为双,目前为空所选择的替代方案是double.NaN。不过,我明白这不是对NaN值的确切的使用目的,所以我不确定是否有这个我不能预见,最好的做法是什么的问题。



我感兴趣的是找出什么是最好的空替代品特别是对于下列数据类型:双/浮点,小数,日期时间,INT /长(虽然别人都更受欢迎)



编辑:我想我需要澄清我对性能的要求。数值数据的演出,通过这些算法在这需要几个小时的时间进行处理。因此,尽管如10毫秒或20毫秒之间的区别通常是微不足道的,在这种情况下它确实令到拍摄的时间显著的影响。


解决方案

好吧,如果你已经排除了可空< T> ,如果只剩下域值 - 即你视为空一个神奇的数字。虽然这不是的理想的,这种情况并不少见要么 - 例如,许多主要的框架代码对待 DateTime.MinValue 相同为空。这至少从共同的价值观......



修改仅强调在没有为NaN

$ B $移动的损害远b

那么,没有 NaN的,也许使用 .MinValue - 但是要记住什么罪恶发生,如果你不小心的使用的相同的值意味着同样数量的...



显然,对于未签名的数据,您需要<$ C $ 。C> .MaxValue (避免零!)



就个人而言,我会尝试使用可空< T> 为表达我的意图更安全......有很多方法可以优化你的可空< T> 代码,也许吧。而且 - 的时候,你已经检查了你需要的所有地方,神奇的数字也许不会比快得多可空< T> <? / p>

I am writing algorithms that work on series of numeric data, where sometimes, a value in the series needs to be null. However, because this application is performance critical, I have avoided the use of nullable types. I have perf tested the algorithms to specifically compare the performance of using nullable types vs non-nullable types, and in the best case scenario nullable types are 2x slower, but often far worse.

The data type most often used is double, and currently the chosen alternative to null is double.NaN. However I understand this is not the exact intended usage for the NaN value, so am unsure whether there are any issues with this I cannot foresee and what the best practise would be.

I am interested in finding out what the best null alternatives are for the following data types in particular: double/float, decimal, DateTime, int/long (although others are more than welcome)

Edit: I think I need to clarify my requirements about performance. Gigs of numerical data are processed through these algorithms at a time which takes several hours. Therefore, although the difference between eg 10ms or 20ms is usually insignificant, in this scenario it really does makes a significant impact to the time taken.

解决方案

Well, if you've ruled out Nullable<T>, you are left with domain values - i.e. a magic number that you treat as null. While this isn't ideal, it isn't uncommon either - for example, a lot of the main framework code treats DateTime.MinValue the same as null. This at least moves the damage far away from common values...

edit to highlight only where no NaN

So where there is no NaN, maybe use .MinValue - but just remember what evils happen if you accidentally use that same value meaning the same number...

Obviously for unsigned data you'll need .MaxValue (avoid zero!!!).

Personally, I'd try to use Nullable<T> as expressing my intent more safely... there may be ways to optimise your Nullable<T> code, perhaps. And also - by the time you've checked for the magic number in all the places you need to, perhaps it won't be much faster than Nullable<T>?

这篇关于替代可空类型在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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