比较好或==哪个具有更好的性能 [英] which has better performance, compare or ==

查看:68
本文介绍了比较好或==哪个具有更好的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个比较快,比较"或"=="

which one is faster, "compare" or "=="

推荐答案

您似乎在问很多模糊而笼统的问题.如果您正在上一门课程,请坐下来并遵循课程结构.如果您正在自学,那么您应该购买一本书并通过这些工作.在这种情况下,简单的答案是,很难想到许多==运算符的速度可能成为性能瓶颈的应用程序.不必太担心此类事情,直到您发现自己在重要的地方,因为它们很少这样做.如今计算机速度很快.

只是添加一下,您应该尝试对问题进行扩展,并解释为什么要在帖子正文中提问,而不仅仅是重复主题行.
You seem to be asking a lot of vague and general questions. If you''re doing a course, sit back and follow the structure of your course. If you''re teaching yourself, then you should buy a book and work through that. In this case, the short answer is, it''s hard to think of many applications where the speed of the == operator is likely to be a bottleneck to performance. Don''t worry too much about such things, until you find yourself in a place where they matter, because they rarely do. Computers are fast nowadays.

Just to add, you should try to expand on your question and explain why you''re asking it in the body of your post, don''t just repeat the subject line.


如果您了解所提到的两种方法的功能,您将知道每种方法都有其自己的工作,两种方法最终都会为您提供相同的结果,但是例如==运算符与值类型一起使用来判断它们是否具有是否具有相同的值,对于引用类型,它返回操作数是否引用相同的对象(在这种情况下,将字符串视为类似于值类型的字符串除外).

通常,您需要将比较功能与自定义比较器一起使用,该自定义比较器将执行某种业务比较或其他功能.

对于性能,我不太确定,但是使用相等运算符的速度更快,因为我认为它足以满足您的需求.
If you understand the functionality of each of the two methods you mentioned you will know that each one has its own work, both some how give you the same result eventually, but for instance the == operator is used with value type to tell if they have the same value or not, and for reference types it returns whether the operands refer to the same object or not (except for string where its treated like value type in this case).

Mostly you will need to use the compare function with a custom comparer that will do some kind of business compare or something.

For performance, I am not pretty sure of that, but using the equality operator is faster as I think if its sufficient for your needs.


这篇关于比较好或==哪个具有更好的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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