DateTime.Now.Ticks一个循环中重复 [英] DateTime.Now.Ticks repeating inside a loop

查看:897
本文介绍了DateTime.Now.Ticks一个循环中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个表的主键一个唯一的ID,我使用 DateTime.Now.Ticks 它。这是一个必要条件,现在我们不能使用身份

I am trying to generate a unique ID for the primary key of a table and I am using DateTime.Now.Ticks for it. That's a requirement for now we can't use Identity.

但有时候,一个循环内它产生于连续迭代相同的ID。
我的简化代码看起来像这样

But sometimes, within a loop it generates same IDs on consecutive iterations. My simplified Code would look like this

    While(IncomingData.Next())
    {
     IncomingData.ID = DateTime.Now.Ticks;
     // Other Operations
      .
      .
      .
     InsertInDatabase(IncomingData);
    }



是不是因为我的处理器的指令/秒的速度比精度更高在该测量?
我用的I5处理器2.9GHZ。虽然我已经通过引入一个计数变量并将其添加到解决我的问题。它不觉得自己是一个不错的办法。反正有人可以打破它为我如何做一个计算是依赖于CPU周期?谢谢。

Is it because my processor's speed for Instructions/sec is greater than the precision of at which Ticks is measured ? I am using I5 2.9GHZ Processor. Though I have solved my problem by introducing a count variable and adding it to ticks. It Doesn't feel like a good approach. Anyway can someone break it down for me as how does a tick is calculated is it dependent on cpu cycle ? Thanks.

推荐答案

在这里我看到你的战略的一些优点是,如果你需要查询数据库特定日期间的数据的唯一情况。否则,简单的整数计数器 - 从0或1开始 - 可能总是会更好。

The only case where I see some merit in your strategy is if you need to query your database for data between specific dates. Otherwise a simple integer counter - starting from 0 or 1 - will probably always be better.

如果实施得当和你的想法是不完全坏使用它的目的是要使用的方法。这可能只是不必要的复杂。

Your idea is not utterly bad if implemented properly and used the way it is meant to be used. It may just be needlessly complicated.

我假设你想你的主键是一个递增的整数,一个合理的要求,与一些数据库快让你的插入。 的GUID将无法工作。
我假设你不能使用自动递增的数据库密钥。
我也假设你写不进你的数据库从多个应用程序 - 也不多台计算机

I am assuming that you want you primary key to be an increasing integer, a sensible requirement to keep your inserts fast with some databases. A GUID will not work for you. I am assuming that you cannot use an auto-incremented database key. I am also assuming that you will not write into your database from multiple applications - nor multiple computers.

首先,你需要采取夏令考虑:使用DateTime.UtcNow而不是DateTime.Now。那是因为DateTime.Now在夏令时向后跳

First, you need to take daylight saving into account: use DateTime.UtcNow instead of DateTime.Now. That's because DateTime.Now can jump backwards in case of daylight saving.

其次,你应该期望DateTime.UtcNow向后反正在罕见的情况下跳 - 当系统时钟调整。这意味着你需要仍要保存先前分配值

Second, you should expect DateTime.UtcNow to jump backwards anyway in rare occasions - when the system clock is adjusted. It means you need to save the previously allocated value anyway.

第三,正如你已经知道,系统时钟的精度不是无限的 - 通常是15毫秒 - ,所以你需要保存先前分配的价值和DateTime.UtcNow两次返回相同的值的情况下增加它。

Third, as you already know, the precision of the system clock is not infinite - typically it is 15 ms -, so you need to save the previously allocated value and increment it in case DateTime.UtcNow returns the same value twice.

四,知道你需要保持一个变量拿着以前分配的价值,为什么不下拉整个日期时间观念并仅在该变量依赖?我的意思是:在你的程序的开始,你可以从数据库中读取的最大价值,将其存储到内存中的计数器,然后增加你需要一个新的键值计数器每次

Fourth, knowing that you will need to keep a variable holding the previously allocated value, why not drop the whole DateTime idea and rely only on that variable? What I mean is: at the start of your program, you could read the greatest value from the database, store it into your counter in memory and then increment the counter everytime you need a new key value.

这篇关于DateTime.Now.Ticks一个循环中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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