Comparison via Equals or HashCode. which is faster?

I have to compare a object with the raw properties of the same class. Meaning, i have to compare those: struct Identifier { string name; string email; } with the two strings name and email. I know i could just create a new Identifier instance for name and email and pass that into equals(). My application has to be very fast and resource-saving. I know that comparison via hashcode i

通过Equals或HashCode比较。 哪个更快?

我必须将对象与同一类的原始属性进行比较。 意思是,我必须比较这些: struct Identifier { string name; string email; } 与两个字符串的名称和电子邮件。 我知道我可以为名称和电子邮件创建一个新的标识符实例,并将其传递给equals()。 我的应用程序必须非常快速且节省资源。 我知道通过hashcode进行比较不是一个好方法,因为这里解释的是碰撞。 但碰撞对我来说没问题,我只需要它快。 所以, 1)通过Ge

Is there a way to derive IEqualityComparer from IComparer?

TL;DR I'm looking for a way to obtain IEqualityComparer<T> from IComparer<T> , no matter which datatype is T , including case-insensitive options if T is string . Or I need a different solution for this problem. Here's full story: I'm implementing simple, generic cache with LFU policy. Requirement is that it must be possible to select whether the cache will be case sen

有没有办法从IComparer派生IEqualityComparer?

TL; DR我正在寻找一种方法从IComparer<T>获取IEqualityComparer<T> ,无论哪个数据类型都是T ,如果T是string ,包括不区分大小写的选项。 或者我需要针对此问题的其他解决方案。 下面是完整的故事:我正在实施简单,通用的缓存与LFU策略。 要求必须能够选择缓存是区分大小写还是不区分大小写 - 如果string恰好是缓存键的数据类型(这不是必需的)。 在我主要开发缓存的解决方案中,我预计有数千亿缓存查找,最

Implementing correct GetHashCode

I have the following class public class ResourceInfo { public string Id { get; set; } public string Url { get; set; } } which contains information about some resource. Now I need the possibility to check if two such resources are equal by the following scenario (I`ve implemented IEquatable interface) public class ResourceInfo : IEquatable<ResourceInfo> { public string Id { g

实现正确的GetHashCode

我有以下课程 public class ResourceInfo { public string Id { get; set; } public string Url { get; set; } } 其中包含有关某些资源的信息。 现在我需要通过以下方案检查两个这样的资源是否相等(我已经实现了IEquatable接口) public class ResourceInfo : IEquatable<ResourceInfo> { public string Id { get; set; } public string Url { get; set; } public bool Equals(ResourceInfo othe

GroupBy on strings (hashcode collision)

I have an array of strings (approx 2000), and I'd like to use IEnumerable.GroupBy to group the equal ones. The problem is though that there are many hash collisions such as "mysteriously" and "well". This is probably due to the fact that GroupBy uses GetHashCode() , which returns an int, which is too small (or the GetHashCode function for the class String hasn't bee

GroupBy对字符串(hashcode碰撞)

我有一个字符串数组(大约2000),并且我想使用IEnumerable.GroupBy来组合相等的。 问题是有很多散列冲突,比如“神秘”和“好”。 这可能是由于GroupBy使用GetHashCode() ,它返回的int太小(或者类String的GetHashCode函数没有很好地实现)。 我想你可以尝试实现一个重写的GetHashCode函数或定义一个自定义IEqualityComparer并使用不同的哈希码,但没有任何方法可以直接或不同地比较它们吗? 我知道这需要更长的时间,但只

What's the role of GetHashCode in the IEqualityComparer<T> in .NET?

I'm trying to understand the role of the GetHashCode method of the interface IEqualityComparer. The following example is taken from MSDN: using System; using System.Collections.Generic; class Example { static void Main() { try { BoxEqualityComparer boxEqC = new BoxEqualityComparer(); Dictionary<Box, String> boxes = new Dictionary<Box,

.NET中的IEqualityComparer <T>中GetHashCode的作用是什么?

我想了解接口IEqualityComparer的GetHashCode方法的作用。 以下示例来自MSDN: using System; using System.Collections.Generic; class Example { static void Main() { try { BoxEqualityComparer boxEqC = new BoxEqualityComparer(); Dictionary<Box, String> boxes = new Dictionary<Box, string>(boxEqC);

Nancy does not render the view nor extending the layout view

I started learning about C# in Mono (Ubuntu) and Nancy which is a web framework for C#. I am using their "super simple view engine" rendering and I cannot make the code to render the .sshtml file, it just shows as plan text file. Ultimately, I want to use a layout file ( layout.sshtml ) and each view would replace part of layout file. I have a hunch that maybe folder structure is no

南希不渲染视图也不扩展布局视图

我开始在Mono(Ubuntu)和Nancy这个C#的Web框架中学习C#。 我正在使用他们的“超级简单视图引擎”渲染,并且我无法使代码呈现.sshtml文件,它仅显示为计划文本文件。 最终,我想使用布局文件( layout.sshtml ),并且每个视图都会替换布局文件的一部分。 我有一个预感,也许文件夹结构无效,例如login.sshtml没有找到layout.sshtml 。 但我修改了.csproj文件以复制视图文件夹: <Target Name="AfterBuild" Condition=

What the operator `<

This question already has an answer here: What is the “-->” operator in C++? 21 answers <-- is not an operator, it is < and -- written without space. Just to make it clear, I rewrite the code like this- while(0 < --i) First the value of i is decreased, and then compared if it is greater than 0 . Similar question was asked here.

什么运营商`<

这个问题在这里已经有了答案: 什么是C ++中的“ - >”运算符? 21个答案 <--不是操作员,它是<和--写入没有空间。 为了说清楚,我重写了这样的代码 - while(0 < --i) 首先减少i的值,然后比较它是否大于0 。 这里提出了类似的问题。

Making a private method public to unit test it...good idea?

Moderator Note: There are already 39 answers posted here (some have been deleted). Before you post your answer, consider whether or not you can add something meaningful to the discussion. You're more than likely just repeating what someone else has already said. I occasionally find myself needing to make a private method in a class public just to write some unit tests for it. Usually th

将私有方法公开给单元测试它......好主意?

版主注意:这里已经有39个答案了(部分已被删除)。 在发布答案之前,请考虑是否可以在讨论中添加有意义的内容。 你很可能只是重复别人已经说过的话。 我偶尔会发现自己需要在公开课上制作私人方法,只为它编写一些单元测试。 通常这是因为该方法包含在类中的其他方法之间共享的逻辑,并且它自己测试逻辑更加整洁,或者另一个原因可能是因为我想测试同步线程中使用的逻辑,而不必担心线程问题。 其他人是否发现他们自己

What are some best practices for reducing memory usage in C?

What are some best practice for "Memory Efficient C programming". Mostly for embedded/mobile device what should be the guidelines for having low memory consumptions ? I guess there should be separate guideline for a) code memory b) data memory In C, at a much simpler level, consider the following; Use #pragma pack(1) to byte align your structures Use unions where a structure ca

什么是减少C语言内存使用的最佳做法?

“Memory Efficient C programming”的一些最佳实践是什么? 大多数情况下,对于嵌入式/移动设备,低内存消耗应该是什么指导原则? 我想应该有单独的指导方针a)代码存储器b)数据存储器 在C中,在更简单的层面上,考虑以下内容; 使用#pragma pack(1)以字节对齐您的结构 在结构可以包含不同类型的数据的情况下使用联合 使用位域而不是整数来存储标志和小整数 避免使用固定长度的字符数组来存储字符串,实现字符串池

When is optimization premature?

I see this term used a lot but I feel like most people use it out of laziness or ignorance. For instance, I was reading this article: http://blogs.msdn.com/b/ricom/archive/2006/09/07/745085.aspx where he talks about his decisions he makes to implement the types necessary for his app. If it was me, talking about these for code that we need to write, other programmers would think either: I

何时优化过早?

我看到这个词用了很多,但我觉得大多数人都是懒惰或无知的人使用它。 例如,我正在阅读这篇文章: http://blogs.msdn.com/b/ricom/archive/2006/09/07/745085.aspx 他在那里谈论他为实现他应用程序所需的类型而作出的决定。 如果是我,谈论这些我们需要编写的代码,其他程序员会认为: 当没有任何东西因此过早地优化时,我想得太多了。 在没有经历减速或性能问题时,过度思考微不足道的细节。 或两者。 并建议实