Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:47:28+00:00 2026-06-13T01:47:28+00:00

I am puzzled by this.. This: for (int j=0; j<100; ++j) { long* data

  • 0

I am puzzled by this.. This:

for (int j=0; j<100; ++j) {
  long* data = new long[0];
}

clock_t launch = clock();
sim.Run();
clock_t done = clock();

runs 50% faster than this alone:

clock_t launch = clock();
sim.Run();
clock_t done = clock();

This is when using -O3. If I use -O0 there is no difference in execution time. Whether it is long or short doesn’t matter. The length of the vector doesn’t change anything, either. I am not using data anywhere. If I delete[] data; in the loop the improvement disappears. As I reduce the number of iterations below 100 the performance gain reduces; above 100 doesn’t make any difference.

If this was Java I would think that I am triggering the GC, but this is c++! Also, it is a single-threaded software so it shouldn’t be memory sharing optimisation stuff.

What can this be? Does this behaviour be a symptom of bad memory management in my code? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:47:29+00:00Added an answer on June 13, 2026 at 1:47 am

    How do you say, “50% longer”? Are we talking minutes, or are we talking seconds?

    In the case of a few seconds, it may simply be a synchronization error (two clock_t may differ up to two seconds without the real time changing more than a few hundredths of a second).

    But my guess is that we’re looking at longer times; and without looking at your code, I suspect that by allocating “leaked” memory you “prime” the memory heap, allowing for faster retrieval of information later on.

    Which tells me that, yes, probably you’re managing your memory less than optimally, and might benefit from pre-allocating and reusing memory (“object pools“).

    With “priming” the memory heap, I mean that memory allocation is usually demanded to a memory manager that keeps track of heap memory; using a linked list in the simpler case. Even if you have no garbage collector, you still have a memory manager which is what lies behind malloc, free, realloc and so on (and new also, for that matter). The MM can operate by requesting a large chunk to the operating system, and then doling it out to the application, and/or by “tweaking” the requests you make in requests that may be better/faster handled by the OS. For example, the OS usually “sees” memory in pages of either 1K, 4K, 64K, depending. If you allocate fifty ten-byte strings on yourself, they might find themselves in different pages and waste lots of memory. The MM on seeing your first request for 10 bytes will maybe allocate 4096, and then parcel them out to you in 10-byte lots.

    Now (I’m going out on a limb here!), suppose your application needs to allocate memory equal to a whole page, in ten chunks. Your initial heap allocation, due to the fact that the program itself needs a small overhead, is a quarter page.

    So you go ahead and allocate your ten chunks. The first six fit in the partially empty page zero; the next four request a new allocation of a new page, page one.

    The allocation done, you start juggling data to and fro your chunks, without ever being aware that they reside in two different pages. Depending on the OS, compiler, optimization and weather forecasts, this might mean that those operations incur an overhead.

    Now let’s suppose that you allocate, and leak, three quarters of a page. Then when you allocate your first chunk, it won’t fit in page zero, and the first – and the remaining nine – chunks all go, and fit, in page one. Should -O3 optimization exploit same-page data access, you will experience a compiler dependent performance gain.

    Please keep in mind that this is only an ad hoc hypothesis. It looks plausible to me, but that’s not really a guarantee of anything 🙂

    More details on libc standard memory management (others exist) here

    http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_3.html#SEC27
    

    You might also check out Google’s tools for C++.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am puzzled why this return false doesn't cancel the post-back. function validate() {
I am really puzzled by this. I believe I am managing memory the correct
okay...im really puzzled with this. I want to create a regular asp.net webforms website
I am a bit puzzled by this strange behavior on CentOs 5.4 when starting
I've been pretty puzzled by this one. It seems as though my implementation of
This really puzzled for hours, I searched all over the internet, but got no
I've been reading on this quite some time...and i'm puzzled - Can you help
This topic has been scratched once or twice, but I am still puzzled. And
I'm still sometimes puzzled when it comes to details of Objective-C. Having this header
I faced this puzzle question[ related to data structure ] in a coding competition.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.