c++ - 模运算与三元运算

标签 c++ c if-statement modulo

我需要迭代 n 对整数:(0, 1), (1, 2), (2, 3) ... (n-2, n-1), (n-1, 0)

最好的方法是什么?

  1. 使用模运算:

    for (int i = 0; i < n; i++){
        int a = i;
        int b = (i + 1)%n
        //MaaaanyLinesOfDoSomethingWithAAndB
    }
    
  2. 使用三元运算:

    for (int i = 0; i < n; i++){
        int a = i;
        int b = (i + 1 == n ? 0 : i + 1)
        //MaaaanyLinesOfDoSomethingWithAAndB
    }
    
  3. 或者:

    for (int i = 0; i < n; i++){
        int a = i;
        int b = (i + 1 >= n ? 0 : i + 1)
        //MaaaanyLinesOfDoSomethingWithAAndB
    }
    
  4. 另一个想法?假设有 maaaany 行做某事,如果我们做 (0, 1), (1, 2), (2, 3) ... (n-2, n-1) 部分和(n-1, 0) 部分分开。

哪种操作效率最高?

编辑#1 对不起,我想我没有正确地问我的问题。我想知道哪个运算符(operator)的 Action 更快(例如,秒或时钟滴答)。我还决定做一些小实验,只用 clock() 函数来测量它。这是我的代码:

#include <time.h>
#include <limits.h>
#include <string>
#include <iostream>
using namespace std;

typedef void (*fun) (int a);

void DoSomething(int i){
    int a = i;
}

void ModuloOperation (int n){
    for (int i = 0; i < n; i++)
        DoSomething((i + 1) % n);
}

void TernaryEqual (int n){
    for (int i = 0; i < n; i++)
        DoSomething(i + 1 == n ? 0 : i + 1);
}

void TernaryBiggerEqual (int n){
    for (int i = 0; i < n; i++)
        DoSomething(i + 1 >= n ? 0 : i + 1);
}

void SplitIntoTwoParts (int n){
    for (int i = 0; i < n - 1; i++)
        DoSomething(i + 1);
    DoSomething(n - 1);
}

int main(){

    const int n = INT_MAX;

    string testNames[] = {
        "Modulo", 
        "Trenary equal", 
        "Trenary bigger equal", 
        "Split into two parts"
    };

    fun tests[] = {
        ModuloOperation, 
        TernaryEqual, 
        TernaryBiggerEqual, 
        SplitIntoTwoParts
    };

    clock_t t;

    for (int i = 0; i < sizeof(testNames)/sizeof(testNames[0]); i++){

        t = clock();
        tests[i](n);
        t = clock() - t;

        cout<<testNames[i]<<": "<<((float)t)/CLOCKS_PER_SEC<<" seconds\n\n";
    }

    return 0;
}

这是一个输出

Modulo: 53.867 seconds

Trenary equal: 36.684 seconds

Trenary bigger equal: 37.299 seconds

Split into two parts: 31.37 seconds

看来p.s.w.g的idea不仅是最干净的,也是最好的。

再次为我的错误道歉,我不是母语人士,我还在学习。

最佳答案

您提到如果您单独执行“maaaany”行,它看起来会很丑陋。以上选项都不是特别漂亮。因此,最好将这种丑陋的逻辑封装在一个方法中,并在循环中使用一些更优雅的代码。

为了可读性,我可能会这样做:

for (int i = 0; i < n - 1; i++){
    DoStuff(i, i + 1);
}
DoStuff(n - 1, 0);

// elsewhere
void DoStuff(int a, int b)
{
    //MaaaanyLinesOfDoSomethingWithAAndB
}

如果 'maaaany' 行需要使用一些局部变量并且您不想将它们全部传递给 DoStuff 方法,您可能需要考虑使用闭包,尽管它不会像适当的功能分解那样帮助提高代码的可读性。像这样:

Action<int, int> doStuff = (a, b) =>
{
    //MaaaanyLinesOfDoSomethingWithAAndB
};

for (int i = 0; i < n - 1; i++){
    doStuff(i, i + 1);
}
doStuff(n - 1, 0);

或者您可能需要将“maaaany”行重构为一个单独的工作类,但如果不了解这些行中的内容,就很难说了。

关于c++ - 模运算与三元运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16844903/

相关文章:

c++ - 使用带有可变参数模板函数的 decltype 的尾随返回类型

c - 如何用socket地址获取自己的IP地址?

if-statement - armv8 NEON 如果条件

php - 如果表 1 中存在值,则插入表 2 mysql

c# - 使用 if 语句缩短 for 循环 - C#

c++ - setter 导致段错误

c++ - 在 C++ 类中初始化二维数组

c++ - XCode3 - 库搜索路径和项目框架之间的关系

c - 如何 : Cross-Operating-System Large File IO in C?

c - 用于从标题中打印出宏值的单行代码