简体   繁体   English

内联 constexpr function 比较“int”和“int”与“const char*”和“const char*”在性能上是否存在理论上的差异?

[英]Is there any theoretical difference in performance in an inline constexpr function that compares an `int` & `int` VS a `const char* & `const char*`?

Is there any theoretical difference in performance in an inline constexpr function that compares an int & int VS a const char* & const char* , when optimization is enabled?启用优化时,比较int & int VS a const char* & const char*inline constexpr function 的性能是否存在理论上的差异?

Example 1 ( int equals int )示例 1( int等于int

struct some_struct {
    int m_type;
    ...
    inline constexpr
    void somefunc() {
        if (m_type == 0) {
            ...
        } else if (m_type == 1) {
            ...
        }
    }
};

Example 2 ( const char* equals const char* )示例 2( const char*等于const char*

struct some_struct {
    const char* m_type;
    ...
    inline constexpr
    void somefunc() {
        if (strcmp(m_type, "some_str_1")) {
            ...
        } else if (strcmp(m_type, "some_str_2")) {
            ...
        }
    }
};

Edit:编辑:

As @RichardCritten pointed out, strcmp is not a constexpr function.正如@RichardCritten 指出的那样, strcmp不是constexpr function。 Though in my actual code I have a custom strcmp function that is a constexpr function.虽然在我的实际代码中,我有一个自定义strcmp function,它是一个constexpr function。

Constexpr functions are computed at compile-time only when required, I mean in constant expression. Consexpr 函数仅在需要时在编译时计算,我的意思是在常量表达式中。

So in constant expression, there are no difference in performance at runtime (compilation time might differ).所以在常量表达式中,运行时的性能没有差异(编译时间可能不同)。

In non-constant expression, functions are computed at runtime as any regular functions (With as-if rule, optimizer might optimize and return a result computed at compilation, constexpr might be a hint for compiler in that regards).在非常量表达式中,函数在运行时与任何常规函数一样计算(使用 as-if 规则,优化器可能会优化并返回编译时计算的结果,在这方面constexpr可能是编译器的提示)。

It depends.这取决于。

Can everytghing be done at compile time?一切都可以在编译时完成吗? Not necessarily.不必要。 If yes, then of course there will be no difference in runtime.如果是,那么运行时当然不会有任何区别。

And for runtime?对于运行时? On some machines and depending on the arictecture a comparison of an int and char* can be the same.在某些机器上,根据架构, intchar*的比较可能是相同的。

This can be found out by looking at the assembler code.这可以通过查看汇编代码来找到。 Still, some CPU internal procedures will also have an impact.尽管如此,一些 CPU 内部程序也会产生影响。

But, comparing an integral type with a C-style character string will rarely have the same performance, because for a string many bytes need to be compared.但是,将整数类型与 C 风格的字符串进行比较很少有相同的性能,因为对于一个字符串,需要比较许多字节。 Also here modern CPU architecury and assembler instructions may help.在这里,现代 CPU 架构和汇编指令也可能有所帮助。

My answer to your question, which seemms to be an xy problem , is:我对您的问题(似乎是xy 问题)的回答是:

Most likely there is a difference, but it depends.很可能存在差异,但这取决于。 . . . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM