Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Michael Angelo Ravera <maravera@prodigy.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Which operation is more expensive?(a <= b and a != 0) |
| Date | 2012-03-07 04:23 -0800 |
| Organization | http://groups.google.com |
| Message-ID | <18348553.0.1331122998753.JavaMail.geo-discussion-forums@pbcua4> (permalink) |
| References | <jigsb4$rdc$1@speranza.aioe.org> <4F4BF9E1.2030702@verizon.net> |
On Monday, February 27, 2012 1:47:13 PM UTC-8, James Kuyper wrote:
> On 02/27/2012 04:24 PM, Ping Cheu wrote:
> > From a web-site that teaches optimization for c,
> >
> > I saw bellow optimization rule.
> >
> > int fact1_func (int n)
> > {
> > int i, fact = 1;
> > for (i = 1; i <= n; i++)
> > fact *= i;
> > return(fact);
> > }
> >
> > int fact2_func(int n)
> > {
> > int i, fact = 1;
> > for (i = n; i != 0; i--)
> > fact *= i;
> > return(fact);
> > }
> >
> > The author say 'fact2_func' is faster than 'fact1_func'.
> >
> > That means 'i <= n' operation is more expensive than 'i != 0'.
> >
> > So far, I thought all condition statements have same cost.
> >
> > So I want to know the cost of condition states from the fast one to slow
> > one.
> >
> > Can anyone tell me?
>
> Yes, someone can tell you. And the answers will be different on
> different machines, and for different compilers on the same machine, and
> for different optimization levels for the same compiler on the same
> machine. The timings will be different depending upon the nature of the
> surrounding code, which will affect how effective the compiler's
> optimization will be. Finally, and most importantly, if there are two
> different but logically equivalent ways of writing your code, the best
> modern compilers will, at high optimization levels, re-write the slower
> one so that it is implemented that same way as the faster one. As a
> result, if you perform a test, you may find that the two loops execute
> equally fast.
>
> In a language like C, which is meant to be widely portable and which
> gives you very poor control over the generated machine code, it's a
> waste of time to acquire that kind of knowledge. It makes more sense to
> worry about such things when you're writing assembly code, where you
> have more precise control over what the generated machine code will look
> like, and a corresponding responsibility to acquire precisely that kind
> of information for the particular machine you're writing the assembly
> code for. You don't expect such code to be portable, so you don't have
> to worry about the fact that the answers will be different on different
> machines.
>
> In C, it makes more sense to worry about higher-level issues - make sure
> your algorithm doesn't waste time doing the same thing twice (or worse,
> the same thing N times, for N very large). Make sure it only does what
> needs to be done, and doesn't waste time and source code doing things
> that don't need to be done. Make sure your algorithm is correct, and
> most important of all, write your code so that it is clearly a correct
> implementation of the algorithm. Let the compiler worry about making it
> fast.
>
> If and only if you find your program has a actual speed problem, run a
> profiler to find out where it is wasting the most time - it's almost
> never the same place that you thought it was, and it's almost never
> involves issues like the loop condition you're worrying about here. Keep
> in mind that the changes you make to speed it up for the current
> platform could make it run slower on some other platform - so you should
> always be very reluctant to make such changes.
... what he said, but you can write the factorial function better than either of these.
1) You have no protection of overflow in either of the above examples.
2) The result isn't correct or useful for negative inputs
3) If you wanted it to be really fast, you'd use a const table (with the table in code space)
4) Write it like this and save a whole iteration (skipping a multiply by what you know will be one and the end of the calculation):
int fact2_func(int n)
{
int i, fact = 1;
for (i = n; i > 1; i--)
fact *= i;
return(fact);
}
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Which operation is more expensive?(a <= b and a != 0) Ping Cheu <nospam@nospam.com> - 2012-02-27 21:24 +0000
Re: Which operation is more expensive?(a <= b and a != 0) James Kuyper <jameskuyper@verizon.net> - 2012-02-27 16:47 -0500
Re: Which operation is more expensive?(a <= b and a != 0) Michael Angelo Ravera <maravera@prodigy.net> - 2012-03-07 04:23 -0800
Re: Which operation is more expensive?(a <= b and a != 0) Keith Thompson <kst-u@mib.org> - 2012-02-27 13:54 -0800
Re: Which operation is more expensive?(a <= b and a != 0) falk@rahul.net (Edward A. Falk) - 2012-02-27 23:47 +0000
Re: Which operation is more expensive?(a <= b and a != 0) Shao Miller <sha0.miller@gmail.com> - 2012-02-27 16:57 -0500
Re: Which operation is more expensive?(a <= b and a != 0) Stephen Sprunk <stephen@sprunk.org> - 2012-02-28 13:43 -0600
Re: Which operation is more expensive?(a <= b and a != 0) Richard Damon <news.x.richarddamon@xoxy.net> - 2012-02-27 16:58 -0500
Re: Which operation is more expensive?(a <= b and a != 0) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-27 13:55 -0800
Re: Which operation is more expensive?(a <= b and a != 0) Kenneth Brody <kenbrody@spamcop.net> - 2012-03-03 15:58 -0500
Re: Which operation is more expensive?(a <= b and a != 0) Keith Thompson <kst-u@mib.org> - 2012-02-27 14:17 -0800
Re: Which operation is more expensive?(a <= b and a != 0) Barry Schwarz <schwarz45@yahoo.com> - 2012-02-27 14:52 -0800
Re: Which operation is more expensive?(a <= b and a != 0) Peter Nilsson <airia@acay.com.au> - 2012-02-27 18:16 -0800
Re: Which operation is more expensive?(a <= b and a != 0) qarnos <darrencubitt@gmail.com> - 2012-02-29 12:20 -0800
Re: Which operation is more expensive?(a <= b and a != 0) Barry Schwarz <schwarzb@dqel.com> - 2012-03-13 22:07 -0700
Re: Which operation is more expensive?(a <= b and a != 0) Kaz Kylheku <kaz@kylheku.com> - 2012-02-28 00:56 +0000
Re: Which operation is more expensive?(a <= b and a != 0) Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-02-27 19:18 -0700
Re: Which operation is more expensive?(a <= b and a != 0) China Blue Sea <chine.bleu@yahoo.com> - 2012-02-27 18:30 -0800
Re: Which operation is more expensive?(a <= b and a != 0) "BartC" <bc@freeuk.com> - 2012-02-28 13:22 +0000
Re: Which operation is more expensive?(a <= b and a != 0) Stephen Sprunk <stephen@sprunk.org> - 2012-02-28 13:38 -0600
Re: Which operation is more expensive?(a <= b and a != 0) jgk@panix.com (Joe keane) - 2012-02-28 22:35 +0000
Re: Which operation is more expensive?(a <= b and a != 0) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-03-03 01:36 +0200
Re: Which operation is more expensive?(a <= b and a != 0) jgk@panix.com (Joe keane) - 2012-03-05 20:03 +0000
Re: Which operation is more expensive?(a <= b and a != 0) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-03-12 01:08 +0200
Re: Which operation is more expensive?(a <= b and a != 0) Harald van Dijk <haraldvdijk@gmail.com> - 2012-03-12 00:05 -0700
Re: Which operation is more expensive?(a <= b and a != 0) James Kuyper <jameskuyper@verizon.net> - 2012-03-12 08:59 -0400
Re: Which operation is more expensive?(a <= b and a != 0) gwowen <gwowen@gmail.com> - 2012-03-12 06:24 -0700
Re: Which operation is more expensive?(a <= b and a != 0) James Kuyper <jameskuyper@verizon.net> - 2012-03-12 10:13 -0400
Re: Which operation is more expensive?(a <= b and a != 0) Keith Thompson <kst-u@mib.org> - 2012-03-12 10:44 -0700
Re: Which operation is more expensive?(a <= b and a != 0) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-03-13 17:12 +0200
Re: Which operation is more expensive?(a <= b and a != 0) Keith Thompson <kst-u@mib.org> - 2012-03-12 11:20 -0700
csiph-web