473,471 Members | 4,687 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Which operation is more expensive?(a <= b and a != 0)

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?
Nov 14 '05 #1
5 2181
zaemin 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?


The ISO standard for C defines the language and its behavior; particular
implementations (and their interaction with the underlying platform)
determine the relative speed of different constructs.

So as far as news:comp.lang.c is concerned, the answer is: *No*.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2


zaemin 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?


It depends on the CPU. The compiler converts source to the machine code of the
target CPU.
So it depends on what op-codes the compiler has to do the job. Usually CPU's
have a special test for zero op-code. So a test for zero may be faster.
Nov 14 '05 #3
In article <f8**************************@posting.google.com >,
za****@orgio.net (zaemin) 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?


No, because it is implementation dependent.

However, good compilers will often manage to transform the code you
wrote into faster code automatically, so if one were for any reason
faster than the other, then the compiler writer would usually have some
way to use the faster method.

With loops, it often helps if the compiler can determine the number of
iterations that the loop will perform before starting to execute it. If
you can write your loop in a way that makes this easy, it will often
help.

Function 1: There are three cases. If n <= 0 there are 0 iterations. If
n < INT_MAX then there are n iterations. If n == INT_MAX there will be
undefined behavior (i will be set to the maximum possible value, and
then incremented again. Not a good idea).

Function 2: There are two cases: If n >= 0 then there are n iterations.
If n < 0 you will have undefined behavior, as you start with negative i,
decrement it until it reaches the most extreme negative value, and then
decrement it further (not a good idea).

Most people write loops using <, <=, >, >= not == or !=, so compilers
are more likely to recognise these patterns and optimise for them. The
typical style is

for (i = 0; i < n; ++i)

which has 0 iterations if n <= 0 and n iterations otherwise. That style
is most likely recognised and produces fast loops.
Nov 14 '05 #4

"Neil Kurzman" <ns*@mail.asb.com> wrote in message
news:42***************@mail.asb.com...


zaemin 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?


It depends on the CPU. The compiler converts source to the machine code
of the
target CPU.
So it depends on what op-codes the compiler has to do the job. Usually
CPU's
have a special test for zero op-code. So a test for zero may be faster.


The second version is ambiguous. For example, it may require an additional
test if(n>0) in order to split the code into a version for the well-behaved
case and one for the ill-behaved case. That forces you into one of the
typical C++ STL performance loss situations. There's no point in attempting
to optimize the loop test for an 8080 when the integer multiplication is
deathly slow on that style of CPU.
Nov 14 '05 #5
za****@orgio.net (zaemin) writes:
From a web-site that teaches optimization for c,
<snip functions>
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?


Read the other replies to this post, and check out the '-S' flag you can
give to gcc (assuming gcc is what you're using). It allows you to see the
assembler code produced by the C compiler. You could inspect the produced
code for each function and see which requires more instructions.

But, as another poster pointed out: /this is not a C language issue/. It
belongs in another forum.

-Denis
Nov 14 '05 #6

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

Similar topics

0
by: Federico Moschini [328594] | last post by:
I have to make difference between2 data from a table in SQL. The page is made with Frontpage, and it extracts QtaGiaCons and Quantita from table "oclrighe". I have to make Quantita -...
1
by: Kashish | last post by:
Is file<<"Some string"<<endl is an atomic operation. where file is an ofstream object. If we output a string in ofstream and we provide endl after that,Can we make sure the whole string will be...
1
by: Ted | last post by:
I need some tips to boost the performance on the following query. The problem is that it times out once in a while, and then again runs normally in most cases. The clue is to compare a textual...
36
by: Vivek Mandava | last post by:
Hello All, Which one is an expensive operation? '>' or '==" ? I know this won't matter much. However, if I'm executing this operation million times, I would prefer the better choice. My...
1
by: I am dog | last post by:
Hi , I use VS 2005 beta1 to do some thing as follows. declare a Dictionary<ulong,DelegateTyep> dictionary = new ... and use dictionary object such as dictionary += aDelegateTyepObject;
1
by: Tomas | last post by:
Is there any sequence diagram on the web that clearly shows in which order all Page methods (load, render and so on) are being called compared to the order the page's contained control methods are...
11
by: tinman | last post by:
Hi... I have the following two excerpts from a method. It basically reads data from a DataReader and loads it in a collection. Excerpt A: ******** With ProjectData
3
by: hayden | last post by:
I'm new to STL, so bear with me... I'm examining a selection of text and storing attributes about the frequency of letter combinations in it. Initially, I assumed I would use a std::map to...
6
by: Christopher | last post by:
I've seen various ways of doing this, but what I want is to be able to take a base class pointer and know which derived class to cast to. For example I am going to make a base light class, that...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.