Re: C++ is slow

From: Aggro (spammerdream_at_yahoo.com)
Date: 03/05/04


Date: Fri, 05 Mar 2004 22:15:57 GMT

Charles Herman wrote:
> I have the following two programs:
>
> PROG I
> void main()

int main()

> {
> int *v = new int[NUM];
> for (int i = 0; i < NUM; ++i)
> v[i] = i;

Did you forget something? delete [] v; perhaps? We don't want to cause
memory leaks, do we?

You will also want to put return 0; at the end of main, because VC 6.0
has a bug and it will complain about int main() if you don't use that. (
It is not required by the standard, but it doesn't matter if you use it.
I personally think that it is even better to use it. )

return 0;

> }
>
> PROG II

> ~rvector() { delete [] vals; }

Note that here you are using delete, unlike you did with the first
program. This might be causing some of the time difference.

> int& operator[] (size_t idx ) { return vals[idx]; }
> };
>
> void main()

int main()

> {
> rvector v(NUM);
> for (i = 0; i < NUM; ++i)
> v[i] = i;

return 0;

> }
>
> I left out some non-germane lines, eg, defining NUM and including header
> files.

Don't do that they might contain some valuable information.



Relevant Pages

  • Re: C++ is slow
    ... > void main ... If you use anything else besides 'int' return type ... > PROG II ...
    (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)
  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • [PATCH] get rid if __cpuinit and __cpuexit
    ... unsigned long action, void *hcpu) ... unsigned int cpu = hcpu; ... -static int __cpuinit ... __cpu_up(unsigned int cpu) ...
    (Linux-Kernel)
  • [PATCH,RFC 2.6.14 09/15] KGDB: SuperH-specific changes
    ... This adds basic support for KGDB on SuperH as well as adding some architecture ... -static int kgdb_uart_getchar ... -static void kgdb_uart_putchar ... * The command-line option can include a serial port specification ...
    (Linux-Kernel)