Вопрос

I have created a C# console app which continuously creates text files with randomly created strings. Time (in minute) is given as an input to the app and it runs for that number of minutes to generate continuous text files. I understand that the amount of data generated will vary depending upon the processor speed and other configurations. My machine is able to generate a total of 25 mb of data in text format in 1 minute.

Now, my question is, can I control the rate of data generated per minute if given as another input to the app through my code?

Any help would be very much appreciated.

Это было полезно?

Решение

var start = DateTime.Now;
int bytesSaved=0;
double desiredSpeed = (25*1024*1024)/60;

while(true)
{
  int latency = bytesSaved/(DateTime.Now-start).TotalSeconds - desiredSpeed;
  if(latency>0) 
    {          
      Thread.Sleep(100);
      continue;
    }

  bytesSaved +=SaveDataChunk(latency);     

}

Другие советы

If you're only able to generate 25MB in a minute, and it's just random strings, then you've got something else to think about - you should easily be able to do orders of magnitude more than that in a minute.

But as far as controlling that, only way I can think of is to manually keep a timer running, and keep track of how much you're writing in how much time, to auto-calculate your rate as you're going - then sleep appropriately if necessary once you compute how fast you're going, vs. how fast you want to go.

It won't be perfect, but you should be able to make a best guess, and continually adjust your sleep time based on past performance.

Yes. You could do one of two things; if you want to control the mb's of the data then have the user pass in an int which you use to control the length of the string written to the file. Another option would be to use a sleep. You could have the user pass in a sleep value and then you could sleep for that amount of time between each write.

You could set up a timer and create a file every N seconds.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top