473,378 Members | 1,507 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,378 software developers and data experts.

function call optimization question

in the code below i thought the function call in g() could be easily
optimized out so that g() becomes the same as h() (which becomes
{return 0;})

executing 'gcc -O3 -S' i found that gcc does not do this

now i'm wondering: is there something in the standard (eg c99) that
prevents this optimization (theoretically)
#include <stdio.h>
#include <stdlib.h>

static inline int c(int a, int b) {
return a == b;
}

static int f(int a, int b, int(*c)(int, int)) {
return c(a, b) - c(b, a);
}

int g(int a, int b) {
return f(a, b, c);
}

int h(int a, int b) {
return c(a, b) - c(b, a);
}
int main(int argc, char *argv[]) {
int a, b;

if (argc < 3)
return printf("usage: %s a b\n", argv[0]);
a = atoi(argv[1]);
b = atoi(argv[2]);
printf("f: %d\n", f(a, b, c));
printf("g: %d\n", g(a, b));
printf("h: %d\n", h(a, b));
return 0;
}

Sep 12 '07 #1
4 1930
Szabolcs Nagy <ns*******@gmail.comwrites:
in the code below i thought the function call in g() could be easily
optimized out so that g() becomes the same as h() (which becomes
{return 0;})

executing 'gcc -O3 -S' i found that gcc does not do this
Well... You can go complain to GCC developers if you like. ;)
now i'm wondering: is there something in the standard (eg c99) that
prevents this optimization (theoretically)
No. Standard doesn't prevents optimisation.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 12 '07 #2

Michal Nazarewicz wrote:
>
No. Standard doesn't prevents optimisation.
thanks for your answers

actually what i was thinking about is the situations like sorting int
arrays

static inline int intcmp(int *a, int *b) {
return *a < *b ? -1: *a *b;
}

void intqsort(int *arr, size_t n) {
qsort(arr, n, sizeof(int), intcmp);
}

imho this is not a silly thing to optimize out, since many algorithms
can be done in a (type) generic way with a couple of function
arguments and most of these algorithms are performance critical (eg
when we cannot allow an additional function call for each int
comparison)

other possible examples:
int find_if(int *arr, size_t n, int (*pred)(int));
int hash_get(const hashtable_t *ht, const key_t *key, int (*hash)
(key_t *), int (*isempty)(item_t *), int (*isdeleted)(item_t *));
....

Sep 12 '07 #3
In article <11**********************@d55g2000hsg.googlegroups .com>,
Szabolcs Nagy <ns*******@gmail.comwrote:
>actually what i was thinking about is the situations like sorting int
arrays

static inline int intcmp(int *a, int *b) {
return *a < *b ? -1: *a *b;
}

void intqsort(int *arr, size_t n) {
qsort(arr, n, sizeof(int), intcmp);
This is unlikely to be useful, because

(a) intcmp is an argument to qsort(), and will be different for different
calls;
(b) even if it wasn't an argument, to inline the calls to intcmp()
its source would have to be available when qsort was compiled,
and typically qsort() is in a pre-compiled library.

One possibility would be for qsort itself to be inline, with its definition
in the header.

If you really need this efficiency, you could take one of the many free
implementations of qsort() and produce a specialised version yourself.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 12 '07 #4

Richard Tobin wrote:
This is unlikely to be useful, because

(a) intcmp is an argument to qsort(), and will be different for different
calls;
(b) even if it wasn't an argument, to inline the calls to intcmp()
its source would have to be available when qsort was compiled,
and typically qsort() is in a pre-compiled library.
true
you are right

well i'm writing an algorithm lib for my own amusement and i thought
it would make things easy if i could write

static void sort_internal(int *arr, size_t len, int (*less)(int, int))
{..}
static inline intless(int a, int b) {return a < b;}

void sort_f(int *arr, int len, int (*less)(int, int)) {
return sort_internal(arr, len, less);
}

void sort(int *arr, int len) {
return sort_internal(arr, len, intless);
}

so i don't need to write down the algorithm 2 times for sort() and
sort_f().
also type generic code can be written in this way so i can make my own
c++ stl like thing.

Sep 12 '07 #5

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

Similar topics

1
by: Vin | last post by:
Is it possible to access a function defined in python shell from c-code? For example if I have defined >>> def c(x): .... return x*x .... and defined in some module foo
3
by: Bryan Parkoff | last post by:
Do C/C++ Compiler allow function to contain more than 8 parameters? I checked MS Visual C++ 6.0 that it can only limit 8 parameters, but most C/C++ Compiler can limit maximum 256 parameters. Can...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
27
by: idoerg | last post by:
Hi all, I am running Python 2.5 on Feisty Ubuntu. I came across some code that is substantially slower when in a method than in a function. ################# START SOURCE ############# # The...
3
by: jack113256 | last post by:
Hi everyone: I have a question in using Callback function, there is my code: /******* code start *********/ #include <stdio.h> void a(); void b(); void run();
10
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
121
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.