gpt4 book ai didi

c++ - C++ 程序真的比类似的 C 程序慢吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:03:08 24 4
gpt4 key购买 nike

<分区>

假设我用 C++ 编写了一个程序,没有使用 RTTI 和运行时多态性(没有虚函数,没有虚继承)并且类没有私有(private)/ protected 成员,也没有使用 C++ 特定的头文件(即使用 C 头文件:cstring, cstdio, ... 而不是 string, iostream, .. .).

然后我想用C写一个类似的程序,函数的第一个参数类型对应于相关的struct

例如:

//C++ code

struct Custom
{
int a;
Custom() { }
void change() { }
~Custom() { }
};

int main()
{
Custom m; //init m
m.change();
//destroy m
}

/*C code*/

struct Custom
{
int a;
};
void custom_init(Custom* like_this) { }
void custom_change(Custom* like_this) { }
void custom_destroy(Custom* like_this) { }

int main()
{
Custom m;
custom_init(&m);
custom_change(&m);
custom_destroy(&m);
}

C++ 程序是否比类似的 C 程序慢(通常)?如果是,为什么 C 程序更快呢?我知道,C++使用RAII设计模式进行内存管理,是不是慢的原因?

我听说有人说 C 程序更快...为什么?

编辑:为什么这个问题被关闭了?我想知道 C++ 是否额外做了一些我们不需要的事情,以及它如何影响性能(让它变慢?变快?还是什么都不做?)。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com