Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pure functions

Reply
Thread Tools

pure functions

 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      06-07-2005
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 ?

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-08-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      06-09-2005

<(E-Mail Removed)> 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.


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      06-09-2005
Malcolm wrote:
>
> <(E-Mail Removed)> 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
 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      06-09-2005
pete <(E-Mail Removed)> wrote:
> Malcolm wrote:
> > <(E-Mail Removed)> 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 (E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-09-2005
"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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-09-2005
pete <(E-Mail Removed)> writes:
> Malcolm wrote:
>> <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      06-10-2005

"Keith Thompson" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      06-11-2005
On Fri, 10 Jun 2005 21:30:57 +0000 (UTC), Malcolm
<(E-Mail Removed)> 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
 
Reply With Quote
 
Anonymous 7843
Guest
Posts: n/a
 
      06-13-2005
In article <(E-Mail Removed)>,
Chris Croughton <(E-Mail Removed)> wrote:
>
>On Fri, 10 Jun 2005 21:30:57 +0000 (UTC), Malcolm
> <(E-Mail Removed)> 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++
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pure space directly inside div ignored, but pure space directlyinside span honored liketofindoutwhy@gmail.com HTML 4 03-29-2008 06:06 PM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
Pure functions still pure after definition Todd Aspeotis C++ 3 05-30-2005 03:53 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Re: Alternate syntax for pure virtual functions? David White C++ 0 08-11-2003 10:11 AM



Advertisments