C ++很慢 [英] C++ is slow
问题描述
我有以下两个程序:
PROG I
void main()
{
int * v = new int [NUM];
for(int i = 0; i< NUM; ++ i)
v [i] = i ;
}
PROG II
class rvector
{
私人:
int * vals;
size_t vsize;
public:
显式rvector (size_t vSize){vals = new int [vsize]; }
~rvector(){delete [] vals; }
int& operator [](size_t idx){return vals [idx]; }
};
void main()
{
rvector v(NUM);
for(i = 0; i< NUM; ++ i)
v [i] = i;
}
我省略了一些非关联线,例如,定义NUM并包括标题
文件。
我编译并运行这些程序使用VC ++ 6.0。第二个程序
所需的时间约为第一个的6倍。发生了什么
在程序II中花了那么多时间?
-charles
I have the following two programs:
PROG I
void main()
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
}
PROG II
class rvector
{
private:
int* vals;
size_t vsize;
public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }
int& operator[] (size_t idx ) { return vals[idx]; }
};
void main()
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v[i] = i;
}
I left out some non-germane lines, eg, defining NUM and including header
files.
I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening
in program II that takes so much time?
-charles
推荐答案
" Charles Herman" < CH ***** @ no.spam>在消息中写道
news:PO62c.120645
"Charles Herman" <ch*****@no.spam> wrote in message
news:PO62c.120645
Xp.533671@attbi_s54 ...
Xp.533671@attbi_s54...
我有以下两个程序:
PROG I
void main()
int main()
如果使用任何东西除了''int''返回类型
for main,你有* no *保证你的程序
的行为。
{
int * v = new int [NUM];
for(int i = 0; i< NUM; ++ i)
v [i] = i;
你有内存泄漏。你需要释放你分配的内存
。
}
PROG II
class rvector
{
private:
int * vals;
size_t vsize;
public:
显式rvector(size_t vSize){vals = new int [vsize]; }
~rvector(){delete [] vals; }
int& operator [](size_t idx){return vals [idx]; }
};
void main()
再次。
{
rvector v( NUM);
for(i = 0; i< NUM; ++ i)
v [i] = i;
}
我遗漏了一些非绅士行,例如,定义NUM并包括头文件。
如果可能的话,你应该总是努力发布*可编辑的*代码。然后我们不必猜测其余部分,这通常是说明问题的原因。
我使用VC ++ 6.0编译并运行这些程序。第二个程序需要大约6倍的运行时间。什么是
在程序II中发生需要花费这么多时间?
I have the following two programs:
PROG I
void main()
int main()
If you use anything else besides ''int'' return type
for main, you have *no* guarantees about the behavior of
your program.
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
You have a memory leak. You need to free the memory
you allocate.
}
PROG II
class rvector
{
private:
int* vals;
size_t vsize;
public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }
int& operator[] (size_t idx ) { return vals[idx]; }
};
void main()
And again.
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v[i] = i;
}
I left out some non-germane lines, eg, defining NUM and including header
files.
You should always strive to post *compilable* code if at all
possible. Then we needn''t guess at the rest, which is often
the cause of a stated problem.
I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening in program II that takes so much time?
C ++语言是一组规范,一个抽象。
我没有'速度',慢或快。显然你发现C ++的特定的实现*(VC ++ 6.0)太慢。
推荐的行动:
阅读您的文档以确保您正确使用该产品,
并找到它的各种模式操作。
如果VC ++被认为是不可接受的,请使用别的东西。
-Mike
The C++ language is a set of specifications, an abstraction.
I has no ''speed'', slow or fast. Apparently you find a particular
*implementation* of C++, (VC++6.0) "too slow".
Recommended actions:
Read your documentation to ensure you''re using the product correctly,
and find it''s various ''modes'' of operation.
If VC++ is deemed unacceptable, use something else.
-Mike
" Mike Wahler" ; < MK ****** @ mkwahler.net>在消息中写道
新闻:OT ****************** @ newsread2.news.pas.earth link.net ...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:OT******************@newsread2.news.pas.earth link.net...
查尔斯赫尔曼 < CH ***** @ no.spam>在消息中写道
新闻:PO62c.120645
"Charles Herman" <ch*****@no.spam> wrote in message
news:PO62c.120645
这篇关于C ++很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!