473,770 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc vs new -> speed of access, virtual memory

Hello there C++ pros..

Well, I have recently redesigned an already existing piece of code
that someone else designed - its just a search algorithm they
developed, and has to perfomr millions of loops depending on the data
input..

My problem is that my code can be upto 10 times slower than the
previous persons - and speed is very critical. (and I dont mean
setting up the arrays takes long, I'm more concernted about the speed
of the search algorithm itself..)

The only difference between my code and the other persons code is that
I am
using subscirpt math to represent 2D arrays using 1D arrays, (so every
access to an array has to do that extra math), also my code is more
object oriented, so more levels of indirection - but I try passing my
classes by reference so that it doesnt have to sepnd extra time
actually copying the data, only time to dereference the address..
ANd finally, I use the new/delete operators instead of malloc/free

I tried mashing my classes together to remove levels of indirection,
but by doing that I had to create more arrays using 'new', and my code
got even slower!!??

Then I decided to get rid of the subscritp math and i created dynamic
2D arrays instead of 1D arrays, and still no difference.

So the algorithm is the same, same number of loops etc, so this is not
the problem,
now the only difference left is that I'm using the new/delete
operators. DO you think this will affect my virtual memory or
something lets say and this is causing the search to slow down?

I've hit a roadblock!
I even profiled using gprof, and the only confuins thing is that its
telling me that 'internal_mcoun t' takes alot of time, i dont know what
the heck this is

this is wat it says:
called/total parents
index %time self descendents called+self name index
called/total children

<spontaneous>
[1] 60.1 198.18 0.00 internal_mcount [1]
0.00 0.00 1/3 atexit [112]

-----------------------------------------------

but yeah anyways, so do you think its the malloc/free new/delete
thing?
your hlep is very apprecieated
htanks!

lost bits =(
Jul 22 '05 #1
4 4359
On 29 Jan 2004 16:58:18 -0800, lo***********@h otmail.com (Lost Bits)
wrote in comp.lang.c++:
Hello there C++ pros..

Well, I have recently redesigned an already existing piece of code
that someone else designed - its just a search algorithm they
developed, and has to perfomr millions of loops depending on the data
input..

My problem is that my code can be upto 10 times slower than the
previous persons - and speed is very critical. (and I dont mean
setting up the arrays takes long, I'm more concernted about the speed
of the search algorithm itself..)

The only difference between my code and the other persons code is that
I am
using subscirpt math to represent 2D arrays using 1D arrays, (so every
access to an array has to do that extra math), also my code is more
object oriented, so more levels of indirection - but I try passing my
classes by reference so that it doesnt have to sepnd extra time
actually copying the data, only time to dereference the address..
ANd finally, I use the new/delete operators instead of malloc/free

I tried mashing my classes together to remove levels of indirection,
but by doing that I had to create more arrays using 'new', and my code
got even slower!!??

Then I decided to get rid of the subscritp math and i created dynamic
2D arrays instead of 1D arrays, and still no difference.

So the algorithm is the same, same number of loops etc, so this is not
the problem,
now the only difference left is that I'm using the new/delete
operators. DO you think this will affect my virtual memory or
something lets say and this is causing the search to slow down?

I've hit a roadblock!
I even profiled using gprof, and the only confuins thing is that its
telling me that 'internal_mcoun t' takes alot of time, i dont know what
the heck this is

this is wat it says:
called/total parents
index %time self descendents called+self name index
called/total children

<spontaneous>
[1] 60.1 198.18 0.00 internal_mcount [1]
0.00 0.00 1/3 atexit [112]

-----------------------------------------------

but yeah anyways, so do you think its the malloc/free new/delete
thing?
your hlep is very apprecieated
htanks!

lost bits =(


The C++ standard does not address the relative speed of any
operations.

If you want to find out what causes the difference in your versions of
your program with your compiler for your operating system with your
compiler options, either use a profiler or create a few test versions.
One that uses 2D arrays with malloc/free and one that uses 1D arrays
with new/delete. That should tell you quickly which change is making
what part of the timing difference under your set up.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2
"Lost Bits" <lo***********@ hotmail.com> wrote in message
news:2b******** *************** ***@posting.goo gle.com...
Hello there C++ pros..

Well, I have recently redesigned an already existing piece of code
that someone else designed - its just a search algorithm they
developed, and has to perfomr millions of loops depending on the data input..

My problem is that my code can be upto 10 times slower than the
previous persons - and speed is very critical. (and I dont mean
setting up the arrays takes long, I'm more concernted about the speed of the search algorithm itself..)

The only difference between my code and the other persons code is that I am
using subscirpt math to represent 2D arrays using 1D arrays, (so every access to an array has to do that extra math), also my code is more
object oriented, so more levels of indirection - but I try passing my classes by reference so that it doesnt have to sepnd extra time
actually copying the data, only time to dereference the address..
ANd finally, I use the new/delete operators instead of malloc/free

I tried mashing my classes together to remove levels of indirection,
but by doing that I had to create more arrays using 'new', and my code got even slower!!??

Then I decided to get rid of the subscritp math and i created dynamic 2D arrays instead of 1D arrays, and still no difference.

So the algorithm is the same, same number of loops etc, so this is not the problem,
now the only difference left is that I'm using the new/delete
operators. DO you think this will affect my virtual memory or
something lets say and this is causing the search to slow down?

I've hit a roadblock!
I even profiled using gprof, and the only confuins thing is that its
telling me that 'internal_mcoun t' takes alot of time, i dont know what the heck this is


As Jack Klein says, the standard doesn't guarantee anything about the
relative speed of malloc and global operator new. In practice,
however, you should not see such a difference. The problem could be
that the C++ uses new much more than the C version used malloc. This
would be fairly typical. Or there may be a lot of temporary objects
created that you are not aware of. There is no way to diagnose the
problem without seeing more of your code.

Jonathan
Jul 22 '05 #3

Lost Bits wrote:
I've hit a roadblock!
I even profiled using gprof, and the only confuins thing is that its
telling me that 'internal_mcoun t' takes alot of time, i dont know what
the heck this is
ignore 'internal_mcoun t'. It is the function inserted(by the compiler)
for gprof when you tell the compiler that you want to prepare the
executable for profiling(e.g -G option in HPUX). So it won't be present
otherwise.

-Amith
C++ Compiler Team
Hewlett Packard

this is wat it says:
called/total parents
index %time self descendents called+self name index
called/total children

<spontaneous>
[1] 60.1 198.18 0.00 internal_mcount [1]
0.00 0.00 1/3 atexit [112]

-----------------------------------------------

but yeah anyways, so do you think its the malloc/free new/delete
thing?
your hlep is very apprecieated
htanks!

lost bits =(


Jul 22 '05 #4
Lost Bits wrote:
Hello there C++ pros.. .... I've hit a roadblock!
I even profiled using gprof, and the only confuins thing is that its
telling me that 'internal_mcoun t' takes alot of time, i dont know what
the heck this is

this is wat it says:
called/total parents
index %time self descendents called+self name index
called/total children

<spontaneous>
[1] 60.1 198.18 0.00 internal_mcount [1]
0.00 0.00 1/3 atexit [112]

-----------------------------------------------

but yeah anyways, so do you think its the malloc/free new/delete
thing?
your hlep is very apprecieated


It'd nearly impossible to help you. Suggestions of profiling are good.

My experience is that C++ code runs just as fast as (usually faster
than) C code if you steer away from std::i/o streams. But you do need
to know up front what is important to optimize for.

Is this code somthing you can post ?

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

59
5201
by: Steve Zimmerman | last post by:
This program compiles fine, but are there any hidden dangers in it, or is it ok? Experiment 1 ################################################## #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h>
13
83901
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
231
23247
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
11
1645
by: Gustavo G. Rondina | last post by:
Hi all I'm writting a simple code to solve an ACM problem (http://acm.uva.es, it is the problem #468). In its code I have the following fragment: freq = calcfreq(hashfreq, strfreq, input); printf("before malloc: %s (%p)\n", input+INPUTLEN); hchars = (char *)malloc(freq*sizeof(char)); schars = (char *)malloc(freq*sizeof(char));
27
4279
by: Chess Saurus | last post by:
I'm getting a little bit tired of writing if (a = malloc(...) == NULL) { // error code } I mean, is it really possible that a malloc call could fail, except in the case of running out of virtual memory? -Chess
7
2216
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
20
10761
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
68
15712
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
23
2730
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef struct pxl { double lon, lat;
3
3624
by: Petr Pavlu | last post by:
Hello, I have two questions how the functions should be written. I read the FAQ but didn't find any answer. If there is any please point me out. I. Cleanup code Consider I have to open file1, then make some malloc and then open file2. What is the best solution how to write the cleanup code? See my pseudo- code ideas.
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.