声明/传递结构与声明/传递单个值 [英] Declaring/passing structs vs. declaring/passing individual values

查看:205
本文介绍了声明/传递结构与声明/传递单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题。但它们之间的关系非常密切。

I have two questions, actually. But they are very closely related.

假设我有一个简单的 struct

public struct TradePrice {
    public float Price;
    public int Quantity;

    public TradePrice(float price, int quantity) {
        Price = price;
        Quantity = quantity;
    }
}



问题1


b $ b

现在如果我有代码需要访问这些值,是否有任何的区别:

TradePrice tp = new TradePrice(50.0f, 100);

并且:

float p = 50.0f;
int q = 100;

我觉得第一个,因为它是调用一个构造函数,应该有类似的开销方法调用。但这只是一个猜测。

I feel like the first, since it is calling a constructor, should have something like the overhead of a method call. But that is just a guess.

上述问题的答案实际上可能回答了这个问题也许有一些微妙的,使这些场景不同;所以我会问。假设我有一个 struct ,它本身有 struct 类型的成员。像这样:

The answer to the above question actually probably answers this question as well, but perhaps there is some subtlety that makes these scenarios different; so I will ask anyway. Suppose I have a struct which itself has members that are struct types. Like this:

public struct BidAsk {
    public TradePrice Bid;
    public TradePrice Ask;
}

那么这三种方法之间有什么区别吗?

Then would there be any difference between these three methods?

Reset(TradePrice bid, TradePrice ask) {
    Bid = bid;
    Ask = ask;
}

Reset(float bidPrice, int bidQuantity, float askPrice, int askQuantity) {
    Bid = new TradePrice(bidPrice, bidQuantity);
    Ask = new TradePrice(askPrice, askQuantity);
}

Reset(float bidPrice, int bidQuantity, float askPrice, int askQuantity) {
    Bid.Price = bidPrice;
    Bid.Quantity = bidQuantity;
    Ask.Price = askPrice;
    Ask.Quantity = askQuantity;
}

我倾向认为有<对于 struct 的构造函数,有一些开销,因此即使在上述方法中传递/设置相同数量的数据,第一个和最后一个可能稍微更有效率。这是真的?第一个和最后一个方法是否不同?

I am inclined to think that there is some overhead to the constructor for a struct, and so even though the same amount of data is being passed/set in the above methods, the first and last ones might be slightly more efficient. Is this true? And are the first and last methods even different?

为了记录,我不是问这个,因为我错误地认为这是造成性能瓶颈或从设计的角度来说,这是非常重要的(虽然也许有人会惊讶我,通过论证 )。我问,因为我只是好奇。

For the record, I'm not asking this because I mistakenly believe this is contributing to a performance bottleneck or that it particularly matters from a design standpoint (though perhaps someone will surprise me by arguing that it does). I'm asking because I'm just curious.

推荐答案

你有更大的问题,先解决。

You've got bigger problems to sort out first. This code smells bad.

问题#1:您使用的是二进制浮点数来表示财务 >数量。二进制浮点数设计为表示科学量,其中最后几位精度中的微小误差不重要。它们极不适合于表示确切的金额。 如果您代表财务数量,始终使用十进制,不要使用浮点。

Problem #1: you're using a binary floating point number to represent a financial quantity. Binary floating point numbers are designed to represent scientific quantities where tiny errors in the last few bits of precisions are unimportant. They are extremely unsuitable for representing exact financial quantities. If you are representing financial quantities always use decimal, never use float.

问题#2:假设您有理由使用二进制浮点,不要使用浮点。使用双。现代硬件优化了做双精度算术,而不是单精度;存在单精度实际上更慢的情况。当然,double占用的内存是float的两倍,但除非你有一百万的内存中的一次,这真的没有关系。此外,你应该使用十进制,这是float的四倍大。

Problem #2: Supposing that you had a good reason to use binary floating point, don't use float. Use double. Modern hardware is optimized to do double precision arithmetic, not single precision; there are situations in which single precision is actually slower. Of course, double takes up twice as much memory as float, but unless you've got a few million of these in memory at once, it really doesn't matter. And besides, you should be using decimal anyway, which is four times larger than float.

问题#3:你有一个可变的值类型。可变值类型是纯邪恶的。很容易意外地引起可变值类型的错误。避免避免避免。

Problem #3: You have a mutable value type. Mutable value types are pure evil. It is so easy to accidentally cause bugs with mutable value types. Avoid avoid avoid.

问题#4:这真的是结构的正确名称吗?我认为交易价格将由Price属性表示。这个东西有更好的名字,更清楚地描述它代表什么吗?它是否代表你的业务领域的一个明确的概念?

Problem #4: Is that really the right name for the struct? I would think that the "trade price" would be represented by the Price property. Is there a better name for this thing that more clearly describes what it represents? Does it represent some clear concept in your business domain?

问题#5:你没有验证传递给公共结构的公共构造函数的参数。这似乎很危险。如果有人试图以-100000.00的价格交易-100股股票。发生了什么?没有什么好的,我敢打赌。

Problem #5: You do no validation on the arguments passed in to a public constructor of a public struct. This seems hazardous. What if someone tries to trade -100 shares at a price of -100000.00 a share. What happens? Nothing good, I'll bet.

问题#6:结构的默认值应该总是结构的有效实例。是吗?此结构的默认实例是Price = 0,Quantity = 0。这是否有意义?如果没有,那么不要使它成为一个结构体。让它成为一个类。

Problem #6: The default value of a struct should always be a valid instance of the struct. Is it? The default instance of this struct is Price = 0, Quantity = 0. Does that actually make sense? If it doesn't, then don't make this a struct. Make it a class.

问题#7:这是逻辑上是一个值类型吗?为什么是一个结构体?这些东西,你觉得应该被逻辑上作为一个值,如数字12,或逻辑上,你可以称为同一件事在多个位置,如客户?如果它不是逻辑上的值,不要使用值类型。

Problem #7: Is this logically a value type in the first place? Why is it a struct? Is this something that you feel should be treated logically as a value, like the number 12, or logically as something you can refer to "the same thing" in multiple locations, like a customer? If it is not logically a value, don't use a value type.

假设名称是正确的,并且这在逻辑上是一个值类型,应该是:如下:

Assuming the name is correct and that this is logically a value type, your code should be something like:

public struct TradePrice 
{ 
    public decimal Price {get; private set;}
    public int Quantity {get; private set; }
    public TradePrice(decimal price, int quantity) : this()
    { 
        if (price < 0m) throw new ...
        if (quantity < 0) throw new ...
        this.Price = price; 
        this.Quantity = quantity; 
    } 
} 

至于您的实际问题: >逻辑差异。有可能是性能差异。我没有最坏的想法,是否有一个可衡量的性能差异在这里或不。你已经写了两种方式的代码。得到一个秒表,运行它十亿次,两种方式,然后你会知道你的问题的答案。

As for your actual questions: there is no logical difference. There might be a performance difference. I haven't got the faintest idea of whether there is a measurable performance difference here or not. You've already writen the code both ways. Get a stopwatch, run it a billion times both ways, and then you'll know the answer to your question.

这篇关于声明/传递结构与声明/传递单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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