Re: Do you use a garbage collector (java vs c++ difference in "new")

Discussion in 'Java' started by Ian Collins, Apr 11, 2008.

  1. Ian Collins

    Ian Collins Guest

    Razii wrote:
    > On Thu, 10 Apr 2008 20:37:59 -0500, Razii
    > <> wrote:
    >
    >> int main(int argc, char *argv[]) {
    >>
    >> clock_t start=clock();
    >> for (int i=0; i<=10000000; i++) {
    >> Test *test = new Test(i);
    >> if (i % 5000000 == 0)
    >> cout << test;
    >> }

    >
    > If I add delete test; to this loop it gets faster. huh? what the
    > exaplanation for this?
    >

    You have just disproved your original hypothesis. Memory is returned to
    the allocator, so it doesn't have to keep fetching more from the system.

    --
    Ian Collins.
     
    Ian Collins, Apr 11, 2008
    #1
    1. Advertising

  2. Ian Collins

    Ian Collins Guest

    Razii wrote:
    > On Fri, 11 Apr 2008 19:35:52 +1200, Ian Collins <>
    > wrote:
    >
    >> You have just disproved your original hypothesis.

    >
    > My number one hypothesis was that "new" is faster in java than in c++.
    > It doesn't behave the same way as in c++. I was speculating that the
    > reason for that is call to operating system in c++.
    >

    You claimed "In C++, each "new" allocation request will be sent to the
    operating system, which is slow."

    You have just disproved this by comparing the performance with and
    without returning memory to the allocator. If each new requested memory
    from the system, the performance would not have changed so
    significantly.

    --
    Ian Collins.
     
    Ian Collins, Apr 11, 2008
    #2
    1. Advertising

  3. Razii wrote:

    > Yes, I did say that based on what I read on a web site. That was his
    > explanation regarding why allocating memory with new is slower in c++.


    Maybe you shouldn't believe everything you read on a web site.
     
    Matthias Buelow, Apr 11, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arne Vajhøj
    Replies:
    12
    Views:
    819
  2. Ian Collins
    Replies:
    0
    Views:
    582
    Ian Collins
    Apr 11, 2008
  3. Roedy Green
    Replies:
    14
    Views:
    1,223
    Mirek Fidler
    Apr 14, 2008
  4. asterisc
    Replies:
    6
    Views:
    508
    Mark Space
    Apr 12, 2008
  5. Juha Nieminen
    Replies:
    10
    Views:
    592
    Mike Schilling
    Apr 13, 2008
Loading...

Share This Page