473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C vs. C++ and C compiler optimization question

/* Consider the following C snippet: */

Foo *pFoo = NULL;

void FooLength(Foo *pFoo);

void FooHello(Foo **ppFoo);

void FooWorld(Foo **ppFoo);

void FooLength(Foo *pFoo) {

int count = 0;

for (Foo *p = pFoo; *p; p++)

++count;

return count;

}

void FooHello(Foo **ppFoo) {

int count = FooLength(*ppFoo);

// ...

}

void FooWorld(Foo **ppFoo) {

int count = FooLength(*ppFoo);

// ...

}

// Consider the following C++ snippet:

class FooHelloWorld {

public:

void FooHello(Foo **ppFoo) {

iCount = FooLength(*ppFoo);

// ...

}

void FooWorld(Foo **ppFoo) {

// no need to recompute iCount

// ...

}

private:

void FooLength(Foo *pFoo) {

int count = 0;

for (Foo *p = pFoo; *p; p++)

++count;

return count;

}

int iCount;

Foo **ppFoo = NULL;

};

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

We could have done the same in C by adding an extra global
variable or function parameter. However, global variables
tend to pollute, so in C we don't tend to keep too many
of them, and too many function parameters can make a
function too hard to understand. On the other hand
in C++ we can place global variables inside classes
so we can cache results easily without the need to
pollute the global namespace or pass many
arguments around.

I wonder if the C compiler could notice that the same
count value is being reused and automatically optimize
by creating a global variable to hold the value
returned from FooCount. My guess is that it
cannot since it does not know whether the
pFoo linked list will be changed between
the invocations of FooHello and FooWorld.

Regards,

JG

Oct 30 '06 #1
1 1254
John Goche wrote:
....
I wonder if the C compiler could notice that the same
count value is being reused and automatically optimize
by creating a global variable to hold the value
returned from FooCount. My guess is that it
cannot since it does not know whether the
pFoo linked list will be changed between
the invocations of FooHello and FooWorld.
Try it and see.

I'm not sure what find of functionality you're looking for but the best
way to handle optimization is to first make sure you need to worry and
then try a few different things to check how the compiler behaves.

The few times I have needed to do an optimization I noticed that the
compiler optimizer has some rather unexpected results.
Oct 30 '06 #2

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

Similar topics

3
by: Nick L. | last post by:
All, This is a general question regarding how, and if, compiler optimization techniques affect the general concept of being able to update a component of an application without requiring a...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
19
by: Jerry | last post by:
I managed to narrow this down to a very simple expression. try this: private void Bug() { bool b = false; Test(3, (b || b) && b && !b); } private void Works() {
44
by: Don Kim | last post by:
Ok, so I posted a rant earlier about the lack of marketing for C++/CLI, and it forked over into another rant about which was the faster compiler. Some said C# was just as fast as C++/CLI, whereas...
32
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: ...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
30
by: lovecreatesbea... | last post by:
K&R says the following in the preface to the first edition, "... the C compiler, and ... are written in C." I'm wondering, does it say even the first / original C compiler was written in C?
29
by: sammy | last post by:
Word up! If there are any gcc users here, maybe you could help me out. I have a program and I've tried compiling it with -O2 and -O3 optimization settings. The wired thing is that it actually...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.