[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Code Optimization

Can the following code be optimized using inline-assembly or in any other way? This portion is in a loop that runs many 10s of thousands of times which causes unacceptable delay. On each loop iteration subRecord is a string of variable size and the idea is to append the new subRecord to sRecord after dynamically re-sizing sRecord. It is obvious that it gets slower as the loop count increases.

//char *subRecord=NULL;
//char *sRecord=NULL;
sRecord = (char*)realloc(sRecord,_msize(sRecord) + (sizeof(char)*(strlen(subRecord))));

1 Solution
use std::vector
arvind_csAuthor Commented:
No, KangaRoo, that does not serve my purpose as I want a concatenated string in the form of sRecord (thousands of characters long) to be passed back to another function as char*. If I use template library, I would have to iterate outside the loop to build my sRecord string after using vector.push_back inside the loop.
Any other ideas?

Seeing as the code snippet does little besides realloc, which is largely an operating system function, there is little to be done with code optimization perse.
The realloc function can return the same block enlarged or a different one. In the latter case it will have to copy the memory contents across.
Altering the algorithm will probably yield better results that optimization.
How about pulling in all the subrecords and mallocing space for them. Then, when you can tell how much total memory you need, malloc one big block for that and copy all the pieces into it.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

The usual way is to pre-allocate a chunk of memory that you think will be big enough.  You will therefore save on
reallocs and it will be more efficient.

Even if you wrote assembler you'd still have to reallocate at some point - unless your first guess was a good one!
malloc() and free() are expensive, reduce the numebr of calls to them
1) use std::string, or
2a) Expand sRecord in very large chunks, sufficient to contain several subRecords.
2b) Reuse the buffer for subRecord, realloc it if it's too small for a particular case, like sRecord
// size_t current_size = _msize(sRecord);
// size_t increment = 16 * _msize(subRecord)
// ...

if(strlen(sRecord) + strlen(subRecord) > current_size)
   sRecord = realloc(sRecord, (current_size += increment));
// etc...
With modern OS and plenty of virtual memory you could indeed allocate huge amounts of memory (100++ MB). Given the sequential way in which the code operates, trashing will be minimal, even less then when you're using realloc().
Here are few ideas.  They might sound stupid but I do not have much to go on like where do the strings come from and so on.

Ok.  You might try to use a second variable that tells you how big of a memory you have allocated and then to start with allocate big chunk.  If this is an option you might get very few realloc.

If you are using MFC you might let CString do allocation for you and keep track of the memory length.  Now when you are done appending you call FreeExtra and now memory is wasted.

I don't where you get subRecord from but if you first copy it into subRecord and then into sRecord you might save some time by resizing first and copying it directly to sRecord by for example making subRecord point to sRecord end.

Hope this helps.

Gunnar Valur


Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now