C# System.DateTime.Now.Ticks%1000>1000在一天中会返回多少次true?
更新 System.DateTime.Now.Ticks%1000>1000每天会返回多少次true 我正在编写一个程序,在静态字典中收集事件的发生情况。我希望定期将字典提交到持久性存储中,但提交的频率太高,因为每个成员中的值的变化率都很高 同事建议使用System.DateTime.Now.Ticks作为随机数,并对该数字进行修改。 我只是不确定如何确定这种情况多久会出现一次 我的班级: 我想让字典每天使用SQL 100次。该词典将更新约250000000次/天,大约1000个键C# System.DateTime.Now.Ticks%1000>1000在一天中会返回多少次true?,c#,.net,datetime,C#,.net,Datetime,更新 System.DateTime.Now.Ticks%1000>1000每天会返回多少次true 我正在编写一个程序,在静态字典中收集事件的发生情况。我希望定期将字典提交到持久性存储中,但提交的频率太高,因为每个成员中的值的变化率都很高 同事建议使用System.DateTime.Now.Ticks作为随机数,并对该数字进行修改。 我只是不确定如何确定这种情况多久会出现一次 我的班级: 我想让字典每天使用SQL 100次。该词典将更新约250000000次/天,大约1000个键 public
public static class StackTraceLogger
{
private static readonly ConcurrentDictionary<string, int> Data = new ConcurrentDictionary<string, int>();
public static void Log(StackTrace st)
{
if (Data.ContainsKey(st.ToString()))
{
var val = 0;
if (Data.TryGetValue(st.ToString(), out val))
{
var newVal = val + 1;
Data.TryUpdate(st.ToString(), newVal, val);
}
}
else
{
Data.TryAdd(st.ToString(), 1);
}
// Periodically log the dictionary to SQL
if (System.DateTime.Now.Ticks % 1000 > 1000)
{
foreach (KeyValuePair<string, int> m in Data)
{
Debugger.Log(m.Value.ToString(), m.Key);
}
}
}
}
所问问题的答案为0 所有x和n的x%n
我不会关注字典以及随机数对您的帮助。此代码将检查自上次更新以来是否超过864秒。864秒在24小时内出现100次 试一试
一天有86400秒。如果你想每天做100次,那么你必须每864秒做一次。最简单的方法是启动一个每864秒发射一次的发射器 例如:
using System.Threading;
public class MyClass
{
// whatever stuff you have
// Timer that will update the data
private Timer _updateTimer;
public MyClass()
{
// initialization
// Start the timer
_updateTimer = new Timer(
UpdateTimerProc, null,
TimeSpan.FromSeconds(864),
TimeSpan.FromSeconds(864));
}
private void UpdateTimerProc(object state)
{
// do SQL update here
}
}
请注意,当计时器每864秒触发一次时,UpdateTimerProc将在一个单独的线程上执行。您用什么语言编程,mod运算符可以返回未定义的结果?假设语言不是古怪的,则为零。如何根据更改的数量提交到永久存储,如果没有写入,则每一个时间间隔回退一次?哦,还有结束节目的时候。你同事的主意是个坏主意。从计时数据中提取随机性是一项只有极少数人知道如何正确完成的任务。如果需要随机数,请使用随机数生成器。System.DateTime.Now.Ticks是一个Int64,可能值得注意的是,如果为负x%n
using System.Threading;
public class MyClass
{
// whatever stuff you have
// Timer that will update the data
private Timer _updateTimer;
public MyClass()
{
// initialization
// Start the timer
_updateTimer = new Timer(
UpdateTimerProc, null,
TimeSpan.FromSeconds(864),
TimeSpan.FromSeconds(864));
}
private void UpdateTimerProc(object state)
{
// do SQL update here
}
}