473,668 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ is slow

I have the following two programs:

PROG I
void main()
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
}

PROG II
class rvector
{
private:
int* vals;
size_t vsize;

public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }

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

void main()
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v[i] = i;
}

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

I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening
in program II that takes so much time?

-charles
Jul 22 '05 #1
20 1680

"Charles Herman" <ch*****@no.spa m> wrote in message
news:PO62c.1206 45$Xp.533671@at tbi_s54...
I have the following two programs:

PROG I
void main()
int main()

If you use anything else besides 'int' return type
for main, you have *no* guarantees about the behavior of
your program.
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
You have a memory leak. You need to free the memory
you allocate.
}

PROG II
class rvector
{
private:
int* vals;
size_t vsize;

public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }

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

void main()
And again.
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v[i] = i;
}

I left out some non-germane lines, eg, defining NUM and including header
files.
You should always strive to post *compilable* code if at all
possible. Then we needn't guess at the rest, which is often
the cause of a stated problem.

I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening in program II that takes so much time?


The C++ language is a set of specifications, an abstraction.
I has no 'speed', slow or fast. Apparently you find a particular
*implementation * of C++, (VC++6.0) "too slow".

Recommended actions:

Read your documentation to ensure you're using the product correctly,
and find it's various 'modes' of operation.

If VC++ is deemed unacceptable, use something else.

-Mike
Jul 22 '05 #2
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:OT******** **********@news read2.news.pas. earthlink.net.. .

"Charles Herman" <ch*****@no.spa m> wrote in message
news:PO62c.1206 45$Xp.533671@at tbi_s54...
I have the following two programs: [snip]
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
[snip]
PROG II
class rvector
{
private:
int* vals;
size_t vsize;

public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }

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

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


You're comparing two different things, naturally their
behavior will often be different. Your 'rvector' class
does more work than your first example.

You're comparing "apples vs oranges".

-Mike
Jul 22 '05 #3
On Fri, 05 Mar 2004 22:03:27 GMT, "Charles Herman" <ch*****@no.spa m> wrote:
I have the following two programs:

PROG I
void main()
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
}

PROG II
class rvector
{
private:
int* vals;
size_t vsize;

public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
I'd look real hard at the spelling of those various version of vsize you
have there. What do you think the value of 'vsize' above really is? ;-)

[plus all of Mike's remarks]
-leor
~rvector() { delete [] vals; }

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

void main()
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v[i] = i;
}

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

I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening
in program II that takes so much time?

-charles


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4
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.
Jul 22 '05 #5
Charles Herman wrote:
...
I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening
in program II that takes so much time?
...


Firstly, I compiled your program with VC6 and run it. Both cycles take
the same time to run, which is not surprising since both cycles are
compiled into identical code. You must be doing something incorrectly,
like testing debugging configuration of the code, which is meaningless.

Secondly, aside from some minor errors (like 'void main'), both programs
are valid C++ programs. Whatever the results are, this test cannot be
used to show that "C++ is slow".

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6
On Fri, 05 Mar 2004 22:12:09 GMT, "Mike Wahler" <mk******@mkwah ler.net>
wrote:


You're comparing two different things, naturally their
behavior will often be different. Your 'rvector' class
does more work than your first example.
You have no idea now /much/ more work... (sorry, Mike, couldn't resist)
-leor

You're comparing "apples vs oranges".

-Mike


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #7
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:t4******** *************** *********@4ax.c om...
On Fri, 05 Mar 2004 22:12:09 GMT, "Mike Wahler" <mk******@mkwah ler.net>
wrote:


You're comparing two different things, naturally their
behavior will often be different. Your 'rvector' class
does more work than your first example.


You have no idea now /much/ more work...


Um yes, I do. (or was that directed at OP?)

-Mike
Jul 22 '05 #8
Mike Wahler wrote:
You're comparing "apples vs oranges".


http://www.inno-vet.com/articles/1999/0599/52.htm

Jul 22 '05 #9
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c2******** *****@news.t-online.com...
Mike Wahler wrote:
You're comparing "apples vs oranges".


http://www.inno-vet.com/articles/1999/0599/52.htm


In his experiment, he chemically and physically changes both
the apple and the orange. So he no longer has an apple or
an orange for comparison.

Also he's protesting that folks are using "apples vs organges"
to denigrate[sic] someone else's analogy. OP did not make an
analogy, I only used one to refute his complaint about
two *different* code examples.
-Mike
Jul 22 '05 #10

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

Similar topics

5
6627
by: yawnmoth | last post by:
using the gethostbyname function seems to noticeably slow down pages. some of the comments in php.net's gethostbyname entry suggest using a version that caches the result, but those versions also only speedup subsequent calls to gethostbyname - the first call is still as slow as ever. so is there anything i can do to speed this up? perhapes i can just implement a function equivalent to gethostbyname using fsockopen, or something?
8
2880
by: Neil | last post by:
I have a very puzzling situation with a database. It's an Access 2000 mdb with a SQL 7 back end, with forms bound using ODBC linked tables. At our remote location (accessed via a T1 line) the time it took to go to a record was very slow. The go to mechanism was a box that the user typed the index value into a combo box, with very simple code attached: with me.RecordsetClone .FindFirst " = " & me.cboGoTo If Not .NoMatch Then Me.Bookmark...
2
3345
by: David | last post by:
Hi, We have an internal network of 3 users. Myself & one other currently have individual copies of the front-end MS Access forms and via our individual ODBC links we have used the: File > Get External Data > Link Tables > select ODBC Databases facility to link to our back-end MySQL Server. On both our machines the tables appear in the window very quickly and if we hit 'Select All', all the tables start loading really quickly into our...
3
2877
by: Jennyfer J Barco | last post by:
In my application I have a datagrid. The code calls a Stored procedure and brings like 200 records. I created a dataset and then a dataview to bind the results of the query to my grid using MyGrid.DataBind() Once the records are loaded, to handle the next, previous button is too slow. I have in the same screen OptionsBox and everytime I click in any option I show some text fields in the screen. Anything the user does is very slow. When...
50
5698
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
0
4437
by: Pratchaya | last post by:
In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log long_query_time=1 #######
2
10905
by: mezise | last post by:
Posted by Pratchaya: ------------------------------------------------------ MySQL Slow Log ERROR In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log
13
3443
by: eighthman11 | last post by:
using Access 2003 and sql server version 8.0 Hey everyone. Created a text box where the user types in an Inventory number and it takes them to that inventory number on the contimuous form. The form is based on a link table to sql server. Here is the code: Dim rst As DAO.Recordset Dim InventoryItem As String InventoryItem = "'" & "TextBoxValue" & "'"
3
2125
by: John | last post by:
Hi I have replaced an ms access app with its vb.net version at a client site. Now the clients keeps complaining about how slow the app response is. The complains they have are for example when app is minimised and then trying to maximise it after a while takes a while for app to get maximised. Also form painting of controls is slow and app is generally slow in terms of response to user clicks. I am using Infragistics controls and my...
10
2346
by: penworthamnaynesh | last post by:
Does php slow your website down? This is what i would like to know.The reason is because my site is writtent 50% in html and 50% in php and is very slow at loading. And i cant tell wether php is doing it or html o is it another reason because i only have 20gb bandwidth My site is called : ultimate city the game http://www.ultimate-gamez.net
0
8374
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
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
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
8575
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
7398
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
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
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.