由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Computation版 - c++为什么比c慢?
相关主题
Matlab逻辑运算符区别一个困惑?
直接用NR的源程序需要注意什么吗?如何用变步长的差分法解PDE?
求解PDE时,用到大型的矩阵,C++里怎么处理?3维的PDE解法,有推荐吗?
LAPACK++ Re: 求解PDE时,用到大型的矩阵,C++里怎么处理?求助: 解有mixed derivative的PDE时遇到麻烦
怎么表示小数点后长度为200的小数呀?[转载] 急问!poisson's PDE analytical solution!
本科学过C++,现在想brush up 求推荐英文教材 (转载)Re: [转载] 急问!poisson's PDE analytical sol
请教一个关于PDE数值解方面的问题[转载] 请教PDE有限差分法的CFL Condition
计算! 计算!求助:二阶PDE的NR数值解法
相关话题的讨论汇总
话题: time话题: c++话题: vector话题: include话题: 格点
进入Computation版参与讨论
1 (共1页)
g****c
发帖数: 20
1
需要模拟一个一维系统,每个格点有三个变量。我把一个格点定义为一个类,然后用
vector<这个类>来实现这个系统;需要做的运算我用重载运算符的方法实现。
同样一个问题,也可以用c来写,用struct定义格点,用一个2维数组(每个数组元素
是一
个struct)来实现这个系统,运算用for loop来实现。
结果是两个程序运算时间差10倍左右,c的很快,c++的很慢(相同格点数),请问是
我用
c++用错了吗?还是vector这些本来比数组慢?
a**********s
发帖数: 588
2
It really depends on how you use C++. Usually C++ is faster than C.

【在 g****c 的大作中提到】
: 需要模拟一个一维系统,每个格点有三个变量。我把一个格点定义为一个类,然后用
: vector<这个类>来实现这个系统;需要做的运算我用重载运算符的方法实现。
: 同样一个问题,也可以用c来写,用struct定义格点,用一个2维数组(每个数组元素
: 是一
: 个struct)来实现这个系统,运算用for loop来实现。
: 结果是两个程序运算时间差10倍左右,c的很快,c++的很慢(相同格点数),请问是
: 我用
: c++用错了吗?还是vector这些本来比数组慢?

p*********t
发帖数: 17
3
Using vector should not be significantly slower than just using C-arrays.
Profile your code to see where the time is spent.
g****c
发帖数: 20
4
I put my c++ code below. The aim is to solve one dimensional multi-variable
PDE. I use a higher-order-time method.
I first defined a class containing three variables, then overloaded all
algorithmic operators to it and also the vector of it. You may skip some
part of the code. The essential part is the evolve function to make the
vector of my class step forward in time.
Thanks in advance for any advice.
#include
#include
#include
#include
#include
【在 p*********t 的大作中提到】
: Using vector should not be significantly slower than just using C-arrays.
: Profile your code to see where the time is spent.

W*****k
发帖数: 158
5
你用vector时有没有从前端删除元素?如果很大量的话,可以考虑用deque。
如果你大概知道vector的长度,可以考虑用reserve(size)

【在 g****c 的大作中提到】
: 需要模拟一个一维系统,每个格点有三个变量。我把一个格点定义为一个类,然后用
: vector<这个类>来实现这个系统;需要做的运算我用重载运算符的方法实现。
: 同样一个问题,也可以用c来写,用struct定义格点,用一个2维数组(每个数组元素
: 是一
: 个struct)来实现这个系统,运算用for loop来实现。
: 结果是两个程序运算时间差10倍左右,c的很快,c++的很慢(相同格点数),请问是
: 我用
: c++用错了吗?还是vector这些本来比数组慢?

e*l
发帖数: 37
6
对于拷贝构造函数no-trivial的对象,函数参数不要按值传递,可以按引用或指
针传递;函数返回值也不要直接返回对象,可以将返回值通过函参传递;尽量避
免临时变量的生成,避免不必要的构造析构开销。你的代码中基本充斥着这些问题,
效率很低是很正常的

variable

【在 g****c 的大作中提到】
: I put my c++ code below. The aim is to solve one dimensional multi-variable
: PDE. I use a higher-order-time method.
: I first defined a class containing three variables, then overloaded all
: algorithmic operators to it and also the vector of it. You may skip some
: part of the code. The essential part is the evolve function to make the
: vector of my class step forward in time.
: Thanks in advance for any advice.
: #include
: #include
: #include

g****c
发帖数: 20
7
I see. But how to avoid creating temporary structures in overloading
operators? In the function below, the variable temp is used to store the
intermediate step of data manipulation. How to get around it? thx
Time operator+(const Time& lhs, const Time& rhs)
{
Time temp = lhs;
temp.seconds += rhs.seconds;
if (temp.seconds >= 60)
{
temp.seconds -= 60;
temp.minutes++;
}
temp.minutes += rhs.minutes;
if (temp.minutes >= 60)
{
temp.minutes -= 60;
temp.hours++

【在 e*l 的大作中提到】
: 对于拷贝构造函数no-trivial的对象,函数参数不要按值传递,可以按引用或指
: 针传递;函数返回值也不要直接返回对象,可以将返回值通过函参传递;尽量避
: 免临时变量的生成,避免不必要的构造析构开销。你的代码中基本充斥着这些问题,
: 效率很低是很正常的
:
: variable

j******n
发帖数: 271
8
No worry about that as compilers are smart enough to optimize away this
temporary if you turn it on.

【在 g****c 的大作中提到】
: I see. But how to avoid creating temporary structures in overloading
: operators? In the function below, the variable temp is used to store the
: intermediate step of data manipulation. How to get around it? thx
: Time operator+(const Time& lhs, const Time& rhs)
: {
: Time temp = lhs;
: temp.seconds += rhs.seconds;
: if (temp.seconds >= 60)
: {
: temp.seconds -= 60;

1 (共1页)
进入Computation版参与讨论
相关主题
求助:二阶PDE的NR数值解法怎么表示小数点后长度为200的小数呀?
请教非线性偏微分方程数值解的求法~~~~本科学过C++,现在想brush up 求推荐英文教材 (转载)
numerical solving PDE on infinite semi-plane请教一个关于PDE数值解方面的问题
a question about 1-D PDE (转载)计算! 计算!
Matlab逻辑运算符区别一个困惑?
直接用NR的源程序需要注意什么吗?如何用变步长的差分法解PDE?
求解PDE时,用到大型的矩阵,C++里怎么处理?3维的PDE解法,有推荐吗?
LAPACK++ Re: 求解PDE时,用到大型的矩阵,C++里怎么处理?求助: 解有mixed derivative的PDE时遇到麻烦
相关话题的讨论汇总
话题: time话题: c++话题: vector话题: include话题: 格点