How would you limit the number of operations per second?
Lets say we have to copy files from one location to another and we don’t want more than 5 files to be processed per second.
Please see what I am doing
private static string currentStamp;
private static int processedInCurrentStamp = 0;
private static void Main(string[] args)
{
currentStamp = DateTime.Now.ToString("{0:d/M/yyyy HH:mm:ss}");
Run();
}
private static void Run()
{
for (int i = 0; i < Int32.MaxValue; i++)
{
string s = DateTime.Now.ToString("{0:d/M/yyyy HH:mm:ss}");
if (currentStamp.Equals(s))
{
if (processedInCurrentStamp < 5)
{
ProcessItem();
processedInCurrentStamp++;
}
}
else
{
Console.WriteLine("{0} ::: {1}", currentStamp, processedInCurrentStamp);
currentStamp = s;
processedInCurrentStamp = 0;
}
}
}
But I need a more elegant and bullet proof way.
Get the starting time, and then in the loop calculate the maximum number of files that should be processed up to the current time, and sleep if you are ahead:
This way the code will catch up if some operations take longer. If you only get three operations one second, it can do seven the next second.
Note that I am using
UtcNowinstead ofNowto avoid the nasty jump in time that happens twice a year.Edit:
Another alternative is to measure the time that an operation takes, and sleep the rest of it’s time slot: