Re: C++ is slow

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 03/05/04


Date: Fri, 05 Mar 2004 22:08:46 GMT


"Charles Herman" <cherman@no.spam> wrote in message
news:PO62c.120645$Xp.533671@attbi_s54...
> 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?

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



Relevant Pages

  • Re: C++ is slow
    ... > void main ... > PROG II ... If it is inlined and not thread safe then even a beginner should be able to ...
    (comp.lang.cpp)
  • Re: C++ is slow
    ... >void main ... >PROG II ... I'd look real hard at the spelling of those various version of vsize you ...
    (comp.lang.cpp)
  • Re: C++ is slow
    ... >> void main ... > If you use anything else besides 'int' return type ... > You have a memory leak. ... >> PROG II ...
    (comp.lang.cpp)
  • Re: C++ is slow
    ... > void main ... memory leaks, do we? ... has a bug and it will complain about int mainif you don't use that. ... > PROG II ...
    (comp.lang.cpp)
  • C++ is slow
    ... void main ... PROG II ...
    (comp.lang.cpp)