Is there a more efficient way of doing the following, something just feels wrong about it? I'm looking for the most time efficient way of logging logarithmically.
public bool Read()
{
long count = Interlocked.Increment(ref _count);
switch (count)
{
case 1L:
case 10L:
case 100L:
case 1000L:
case 10000L:
case 100000L:
case 1000000L:
case 10000000L:
case 100000000L:
case 1000000000L:
case 10000000000L:
case 100000000000L:
case 1000000000000L:
case 10000000000000L:
case 100000000000000L:
case 10000000000000000L:
case 100000000000000000L:
case 1000000000000000000L:
_logger.LogFormattable(LogLevel.Debug, $"{count} Rows Read");
break;
}
return _reader.Read();
}
Update:
Here are my micro benchmark tests.
- Method1: Übercoder's way with keep up with state
- Method2: My way with the big switch statement
- Method3: Markus Weninger's way with the nice Math Function
Since for me 100,000,000 records being read without logging takes around 20 minutes then an extra 4 seconds is nothing. I'm going with the beautiful Math way of doing things. Mathod3 wins in my scenario.
Run time for 100,000,000 iterations averaged over 100 attempts
Method1 Max: 00:00:00.3253789
Method1 Min: 00:00:00.2261253
Method1 Avg: 00:00:00.2417223
Method2 Max: 00:00:00.5295368
Method2 Min: 00:00:00.3618406
Method2 Avg: 00:00:00.3904475
Method3 Max: 00:00:04.0637217
Method3 Min: 00:00:03.2023237
Method3 Avg: 00:00:03.3979303