c# - 以对数方式记录 1、10、100、1000 等

标签 c# .net logarithm

是否有更有效的方法来执行以下操作,感觉有些不对劲?我正在寻找最省时的对数记录方式。

    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();
    }

更新:

这是我的微基准测试。

  • 方法 1:Übercoder 跟上状态的方式
  • 方法 2:我使用大 switch 语句的方式
  • 方法 3:Markus Weninger 的数学函数方法

因为对我来说,读取 100,000,000 条记录而无需记录大约需要 20 分钟,那么额外的 4 秒算不了什么。我要用漂亮的数学方式做事。 Mathod3 在我的场景中获胜。

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

最佳答案

如果性能不是大问题,我会使用下面的

if(Math.Log10(count) % 1 == 0)
  _logger.LogFormattable(LogLevel.Debug, $"{count} Rows Read");

This question陈述如下:

For floating point numbers, n % 1 == 0 is typically the way to check if there is anything past the decimal point.


编辑:为了完成我的回答,还可以跟踪下一个日志记录值,正如@Übercoder 在他的回答中发布的那样。

long nextLoggingValueForLogX = 1;
if (count == nextLoggingValueForLogX )
{
   nextLoggingValueForLogX *= 10; // Increase it by your needs, e.g., logarithmically by multiplying with 10
   _logger.LogFormattable(LogLevel.Debug, $"{count} Rows Read");
}

然而,此方法将为不应每次都执行的每个日志提供一个新变量。如果必须使其成为线程安全的,这将引入额外的代码和额外的工作。

关于c# - 以对数方式记录 1、10、100、1000 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35373163/

相关文章:

c# - 在 C# 中获取以下命令行参数(数组中的元素)

C# 和委托(delegate)

java - WPF是不是一个了不起的东西?

Neo4j:在 Cypher 中使用 Log 函数

c++ - 如何快速计算任何底数的整数对数?

c# - 表达式树 :How to check if index exists?

c# - 如何在 SpannableString Android 中设置不透明度/alpha

c# - 锁 block 内的异常

c# - 我的用户控件应该只添加到某种类型的控件

highcharts - 具有 0 值的对数图表