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;
|