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

pure functions

I have read certain articles that encourage to use/write
pure functions (if possible) as they are better suited for
optimization.

I got one example that expalins how the code can be optimised.
For eg:

__pure int square(int x)
{
return x * x;
}

int f(int n)
{
return square(n) + square(n);
}

The function f() can be optimised to call pure function square() once.

Is there any other way pure functions help in optimization ?
Is the pure function itself produce much optimized code as compared
to non pure functions ?

Nov 14 '05 #1
13 2279
ju**********@yahoo.co.in writes:
I have read certain articles that encourage to use/write
pure functions (if possible) as they are better suited for
optimization.

I got one example that expalins how the code can be optimised.
For eg:

__pure int square(int x)
{
return x * x;
}

int f(int n)
{
return square(n) + square(n);
}

The function f() can be optimised to call pure function square() once.

Is there any other way pure functions help in optimization ?
Is the pure function itself produce much optimized code as compared
to non pure functions ?


You do know there's no "__pure" keyword in standard C (or any other
mechanism for specifying that a function is "pure"), don't you?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #2

<ju**********@yahoo.co.in> wrote
I have read certain articles that encourage to use/write
pure functions (if possible) as they are better suited for
optimization.

pure functions are a good idea, but efficiency considerations wouldn't be
high on my list of reasons why.
Nov 14 '05 #3
Malcolm wrote:

<ju**********@yahoo.co.in> wrote
I have read certain articles that encourage to use/write
pure functions (if possible) as they are better suited for
optimization.

pure functions are a good idea,
but efficiency considerations wouldn't be
high on my list of reasons why.


Pure functions aren't part of C.
This off topic thread has gone on way too long.

--
pete
Nov 14 '05 #4
pete <pf*****@mindspring.com> wrote:
Malcolm wrote:
<ju**********@yahoo.co.in> wrote
I have read certain articles that encourage to use/write
pure functions (if possible) as they are better suited for
optimization.
pure functions are a good idea,
but efficiency considerations wouldn't be
high on my list of reasons why.


I hear some people even say that efficiency considerations are
off-topic altogether in c.l.c. OTOH they are also one of the most
important factors for acceptance of new features into the C language.
I don't understand why many people here escape speed questions.
Is "ignore `restrict'" the only on-topic answer here?
I know that C does not make any specific guarantees in this area,
but some C constructs are designed to be better (or not worse)
than others. Wouldn't discussing opportunities for optimization
(or which constructs to avoid) be on-topic in c.l.c.?
Pure functions aren't part of C.
True, but such feature could one day become part of C (IMHO it is
a good candidate). I don't mean to change the subject of the group,
but couldn't preliminary discussions of new features in context of
present language rules start here, before they're brought to c.s.c
for a more serious treatment? After all, OP did not ask
about a specific implementation, but asked for possibilities
of optimization of pure functions in general (presumably within
the current C framework).
This off topic thread has gone on way too long.


Technically this is probably off-topic, true. But wouldn't
a discussion of the `restrict' keyword, as implemented on my
favourite free C compiler, be off-topic on 30 Nov 1999, and
on-topic the day after?

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #5
"S.Tobias" wrote:
.... snip ...
I hear some people even say that efficiency considerations are
off-topic altogether in c.l.c. OTOH they are also one of the most
important factors for acceptance of new features into the C language.
I don't understand why many people here escape speed questions.
Is "ignore `restrict'" the only on-topic answer here?
I know that C does not make any specific guarantees in this area,
but some C constructs are designed to be better (or not worse)
than others. Wouldn't discussing opportunities for optimization
(or which constructs to avoid) be on-topic in c.l.c.?


You have a point, especially when it comes to embedded or other
small systems. But by and large the compiler is usually better
able to optimize than you are, as long as the coding intent is
clear and accurate. 99.95% of the optimization questions just
shouldn't be asked at all. 50% of the remaining ones are answered
by moving a strlen call out of the loop, because you know the
returned value won't change and the compiler doesn't.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #6
pete <pf*****@mindspring.com> writes:
Malcolm wrote:
<ju**********@yahoo.co.in> wrote
>I have read certain articles that encourage to use/write
> pure functions (if possible) as they are better suited for
> optimization.
>

pure functions are a good idea,
but efficiency considerations wouldn't be
high on my list of reasons why.


Pure functions aren't part of C.
This off topic thread has gone on way too long.


There's no feature in C that can be used to mark a function as "pure",
but it's certainly possible to write pure functions in C. If an
optimizing compiler can determine that a given function is "pure", it
can replace some calls with cached values, or eliminate calls whose
results aren't used, an optimization permitted by the "as-if" rule.

Discussion of pure functions within the context of standard C, and the
opportunities they can provide for potential optimizations, is
topical. Discussion of implementation-specific features to support
pure functions are off-topic (<OT>I'll take the liberty of mentioning
gcc's __attribute__ ((pure)), but I won't go into detail</OT>).
Advocacy of such features for future standards is probably more
topical in comp.std.c, but I see no real problem with bringing it up
here.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

"Keith Thompson" <ks***@mib.org> wrote

Discussion of pure functions within the context of standard C, and the
opportunities they can provide for potential optimizations, is
topical. Discussion of implementation-specific features to support
pure functions are off-topic (<OT>I'll take the liberty of mentioning
gcc's __attribute__ ((pure)), but I won't go into detail</OT>).
Advocacy of such features for future standards is probably more
topical in comp.std.c, but I see no real problem with bringing it up
here.

Discussion about how to use C effectively, so that we can enjoy the benefits
of pure functions despite the fact that the language provides no way to
enforce lack of side-effects, is also topical.
For instance you could have a convention that pure functions always take
single-letter parameters, non-pure functions always take at least one
parameter with more than one letter.
Nov 14 '05 #8
On Fri, 10 Jun 2005 21:30:57 +0000 (UTC), Malcolm
<re*******@btinternet.com> wrote:
For instance you could have a convention that pure functions always take
single-letter parameters, non-pure functions always take at least one
parameter with more than one letter.


Or no parameters at all (a function which has no parameters can't be
'pure' unless it is trivial and returns a constant).

Chris C
Nov 14 '05 #9
In article <sl******************@ccserver.keris.net>,
Chris Croughton <ch***@keristor.net> wrote:

On Fri, 10 Jun 2005 21:30:57 +0000 (UTC), Malcolm
<re*******@btinternet.com> wrote:
For instance you could have a convention that pure functions always take
single-letter parameters, non-pure functions always take at least one
parameter with more than one letter.


Or no parameters at all (a function which has no parameters can't be
'pure' unless it is trivial and returns a constant).


There are also "short term" pure functions like time() and ftell()
which normally cannot be treated as pure by the compiler, but which
are nonetheless good candidates for manually hoisting out of tight loops
in which nothing in the loop resets the date or writes to the file.
--
7842++
Nov 14 '05 #10
In article <4Gjre.52$X71.31@fed1read07>,
Anonymous 7843 <an******@example.com> wrote:
There are also "short term" pure functions like time() and ftell()
which normally cannot be treated as pure by the compiler, but which
are nonetheless good candidates for manually hoisting out of tight loops
in which nothing in the loop resets the date or writes to the file.


That's an interesting idea; I'm not sure that the examples are
the best ones, though.

time() deals in terms of seconds, not in terms of "dates"; any
loop that operates for long enough is going to tick over to a new
second. One could imagine an algorithm that sleep() until just before
an action was to be taken, and then went into a loop polling time()
until the crossover. (The algorithm should, of course, check that
the time() is returning a valid value, for the cases where there is
no real-time clock.)
ftell()... one should look at signals for this one. One cannot
do much within a pure C signal handler routine, but C does not
prohibit implementation extensions such as POSIX.1 . In POSIX.1,
two of the routines that are considered "signal safe" (allowed to
execute them inside a signal handler) are close() and write().
Both of those operate on file descriptors, not on FILE*, but
if I'm interpreting correctly, close() on the file descriptor
"underlying" a FILE* has an effect on the FILE*'s state.

The POSIX.1 1990 language about the connection between file descriptors
and FILE* is not the clearest, so I'm not certain of the exact POSIX.1
proper behaviour in the case of close() and write() on the
underlying descriptors... plausibly the FILE* level wouldn't properly
catch on until the next fseek() [or until sufficient data was
read to exhaust the in-memory buffer, thus calling upon the underlying
read() routine that would notice the end-of-file.] We'd need someone
well versed in POSIX.1 to detail this situation for us.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 14 '05 #11
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <4Gjre.52$X71.31@fed1read07>,
Anonymous 7843 <an******@example.com> wrote:
There are also "short term" pure functions like time() and ftell()
which normally cannot be treated as pure by the compiler, but which
are nonetheless good candidates for manually hoisting out of tight loops
in which nothing in the loop resets the date or writes to the file.


That's an interesting idea; I'm not sure that the examples are
the best ones, though.

time() deals in terms of seconds, not in terms of "dates"; any
loop that operates for long enough is going to tick over to a new
second. One could imagine an algorithm that sleep() until just before
an action was to be taken, and then went into a loop polling time()
until the crossover. (The algorithm should, of course, check that
the time() is returning a valid value, for the cases where there is
no real-time clock.)


time() deals in terms of time_t, an arithmetic type capable of
representing times. The standard doesn't specify the resolution of
time_t or of the time() function; 1 second is common, but not
required.

An aggresively optimizing compiler presumably can know what the
resolution is, but I'm still skeptical that optimizing away a call to
time() would be worth the effort.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #12
On Mon, 13 Jun 2005 17:48:48 +0000, Anonymous 7843 wrote:
In article <sl******************@ccserver.keris.net>,
Chris Croughton <ch***@keristor.net> wrote:

On Fri, 10 Jun 2005 21:30:57 +0000 (UTC), Malcolm
<re*******@btinternet.com> wrote:
For instance you could have a convention that pure functions always take
single-letter parameters, non-pure functions always take at least one
parameter with more than one letter.


Or no parameters at all (a function which has no parameters can't be
'pure' unless it is trivial and returns a constant).


There are also "short term" pure functions like time() and ftell()
which normally cannot be treated as pure by the compiler, but which
are nonetheless good candidates for manually hoisting out of tight loops
in which nothing in the loop resets the date or writes to the file.


However taking time() out of the loop can produce different results. It
isn't a matter of assuming that the results will be the same, it is
whether the potentially different results can still be considered correct
in context. They may even be preferable.

On some platforms a file position can be altered by something other than
the executing program.

Lawrence


Nov 14 '05 #13
In article <pa****************************@netactive.co.uk> ,
Lawrence Kirby <lk****@netactive.co.uk> wrote:

On Mon, 13 Jun 2005 17:48:48 +0000, Anonymous 7843 wrote:
In article <sl******************@ccserver.keris.net>,
Chris Croughton <ch***@keristor.net> wrote:

On Fri, 10 Jun 2005 21:30:57 +0000 (UTC), Malcolm
<re*******@btinternet.com> wrote:

For instance you could have a convention that pure functions always take
single-letter parameters, non-pure functions always take at least one
parameter with more than one letter.

Or no parameters at all (a function which has no parameters can't be
'pure' unless it is trivial and returns a constant).
There are also "short term" pure functions like time() and ftell()
which normally cannot be treated as pure by the compiler, but which
are nonetheless good candidates for manually hoisting out of tight loops
in which nothing in the loop resets the date or writes to the file.


However taking time() out of the loop can produce different results. It
isn't a matter of assuming that the results will be the same, it is
whether the potentially different results can still be considered correct
in context. They may even be preferable.


I should have used different words: by "manually" I meant "as
performed by a human not by the compiler". "hoist" in this
context however is usually meant as an optimization performed
by the compiler, so I apologize for the confusion.

Obviously, if the compiler wanted to do such a thing it would have to
be quite careful about the conditions, or even be specifically
told (e.g. gcc -O9) that it's okay to make aggressive
assumptions about short-term functiony pure-ness.
On some platforms a file position can be altered by something other than
the executing program.


(ITYM file length, but point taken.)

So any program that uses time() or ftell() is already
non-deterministic. Extra out-of-dateness from my proposed
trick would only make the problem worse, but would not
make a deterministic program non-deterministic.
--
7842++
Nov 14 '05 #14

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

Similar topics

11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
0
by: Kevin Bracey | last post by:
I've been implemening Annex F in my compiler, and one issue to do with "pure" functions has come to my attention. The compiler has for a long time had a __pure keyword (and a related pragma...
3
by: junky_fellow | last post by:
I have read certain articles that encourage to use/write pure functions (if possible) as they are better suited for optimization. I got one example that expalins how the code can be optimised....
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
10
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.