栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

GUID不是唯一的简单证明

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

GUID不是唯一的简单证明

Kai,我提供了一个程序,该程序可以使用线程来完成您想做的事情。它根据以下条款获得许可:您必须为运行它的每个CPU内核每小时向我支付$
0.0001。费用在每个日历月末支付。请尽快与我联系以获取我的Paypal帐户详细信息。

using System;using System.Collections.Generic;using System.Linq;namespace GuidCollisionDetector{    class Program    {        static void Main(string[] args)        { //var reserveSomeRam = new byte[1024 * 1024 * 100];     // This indeed has no effect. Console.WriteLine("{0:u} - Building a bigHeapOGuids.", DateTime.Now); // Fill up memory with guids. var bigHeapOGuids = new HashSet<Guid>(); try {     do     {         bigHeapOGuids.Add(Guid.NewGuid());     } while (true); } catch (OutOfMemoryException) {     // Release the ram we allocated up front.     // Actually, these are pointless too.     //GC.KeepAlive(reserveSomeRam);     //GC.Collect(); } Console.WriteLine("{0:u} - Built bigHeapOGuids, contains {1} of them.", DateTime.Now, bigHeapOGuids.LongCount()); // Spool up some threads to keep checking if there's a match. // Keep running until the heat death of the universe. for (long k = 0; k < Int64.MaxValue; k++) {     for (long j = 0; j < Int64.MaxValue; j++)     {         Console.WriteLine("{0:u} - Looking for collisions with {1} thread(s)....", DateTime.Now, Environment.ProcessorCount);         System.Threading.Tasks.Parallel.For(0, Int32.MaxValue, (i) =>         {  if (bigHeapOGuids.Contains(Guid.NewGuid()))      throw new ApplicationException("Guids collided! Oh my gosh!");         }         );         Console.WriteLine("{0:u} - That was another {1} attempts without a collision.", DateTime.Now, ((long)Int32.MaxValue) * Environment.ProcessorCount);     } } Console.WriteLine("Umm... why hasn't the universe ended yet?");        }    }}

PS:我想尝试一下Parallel扩展库。那很简单。

并且使用OutOfMemoryException作为控制流只是感觉不对。

编辑

好吧,看来这仍然吸引了选票。所以我已经解决了GC.KeepAlive()问题。并将其更改为与C#4一起运行。

为了阐明我的支持条款:仅在2010年2月28日提供支持。请仅使用时间机器在当天发出支持请求。

编辑2 与往常一样,GC在管理内存方面比我做得更好。我自己以前做过的任何尝试都注定要失败。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/442818.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号