473,651 Members | 3,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fast addition for n+1 or n+0

Consider the following statement:
n+i, where i = 1 or 0.

Is there more fast method for computing n+i than direct computing that sum?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Jul 23 '05 #1
24 2703
* Alex Vinokur:
Consider the following statement:
n+i, where i = 1 or 0.

Is there more fast method for computing n+i than direct computing that sum?


That depends on the types involved.

For built-in numeric types, direct computation is probably fastest.

Measure if you're in doubt (and it really matters).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
"Alex Vinokur" <al****@big-foot.com> writes:
Consider the following statement:
n+i, where i = 1 or 0.

Is there more fast method for computing n+i than direct computing that sum?


The best way to compute n+0 is n.

The best way to compute n+1 is n+1; if the CPU provides something
faster than a general add instruction, the compiler will generate it
for you.

--
Keith Thompson (The_Other_Keit h) 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.
Jul 23 '05 #3
Alex Vinokur wrote:
Consider the following statement:
n+i, where i = 1 or 0.

Is there more fast method for computing n+i than direct computing that
sum?


Assuming integers, hardware addition is implemented simply using full
adders, or faster algorithms like carry lookahead.

n+0 has no carries is is fast; many compliers will constant fold to n
n+1 has potentially m carries in m-bit arithmetic

Full adder:
http://isweb.redwoods.cc.ca.us/INSTR...logic/full.htm

Carry look ahead:
http://www.seas.upenn.edu/~ee201/lab...kAheadF01.html
gtoomey
www.gregorytoomey.com
Jul 23 '05 #4
In article <37************ *@individual.ne t>,
Alex Vinokur <al****@big-foot.com> wrote:
Consider the following statement:
n+i, where i = 1 or 0.

Is there more fast method for computing n+i than direct computing that sum?


Assuming n and i are ints, not on a modern general purpose computer.
Addition typically takes one cycle, once the operands are in
registers.

Any attempt to use a conditional will almost certainly be much slower.

For more details, try a newsgroup for the processor you're interested
in, or maybe comp.arch.

-- Richard
Jul 23 '05 #5

"Richard Tobin" <ri*****@cogsci .ed.ac.uk> wrote in message news:cv******** ***@pc-news.cogsci.ed. ac.uk...
In article <37************ *@individual.ne t>,
Alex Vinokur <al****@big-foot.com> wrote:
Consider the following statement:
n+i, where i = 1 or 0.

Is there more fast method for computing n+i than direct computing that sum?


Assuming n and i are ints, not on a modern general purpose computer.
Addition typically takes one cycle, once the operands are in
registers.

Any attempt to use a conditional will almost certainly be much slower.

For more details, try a newsgroup for the processor you're interested
in, or maybe comp.arch.

-- Richard


I need that in C/C++ program.

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #6


Alex Vinokur wrote:
"Richard Tobin" <ri*****@cogsci .ed.ac.uk> wrote in message news:cv******** ***@pc-news.cogsci.ed. ac.uk...
In article <37************ *@individual.ne t>,
Alex Vinokur <al****@big-foot.com> wrote:

Consider the following statement:
n+i, where i = 1 or 0.

Is there more fast method for computing n+i than direct computing that sum?


Assuming n and i are ints, not on a modern general purpose computer.
Addition typically takes one cycle, once the operands are in
registers.

Any attempt to use a conditional will almost certainly be much slower.

For more details, try a newsgroup for the processor you're interested
in, or maybe comp.arch.


I need that in C/C++ program.


Well, there is no general truth helping you along to a portable,
always perfect solution.
If you want to optimise your code for speed, use a profiler to
determine which functions are called how often and take how much
time. Then you know _where_ you lose your time.
After that, try to find algorithms which reduce the number
of calls to small functions which take a good part of the overall
time and reduces the time spent in "big" functions taking much time.
If you afterwards really find that optimising code with
'n+0' and 'n+1' would be the best possible micro-optimisation
to gain some more cycles, then you should try to write as many
'n+0's/'n's and 'n+1's as possible explicitly in your code
instead of using 'n+i'. The compiler will optimise that if the
code has the potential for optimisation.
Afterwards, use the profiler to determine whether this actually
makes a difference.

Probably not much.
If you think you can do better than the compiler, then follow
Richard's suggestion about comp.arch.*
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Jul 23 '05 #7
In article <37************ *@individual.ne t>,
Alex Vinokur <al****@big-foot.com> wrote:
:Consider the following statement:
:n+i, where i = 1 or 0.

:Is there more fast method for computing n+i than direct computing that sum?

It depends on the costs you assign to the various operations -- a
matter which is architecture dependant. Integer addition is usually one of
the fastest things a computer does. Suppose you were able to find a
two instruction sequence that was faster for that particular case: then
it is very likely to be slower because internally the CPU has
to perform an integer addition in order to find the address of the
second instruction.

Have you perhaps omitted some important facts about the circumstances?
For example, are you microprogrammin g, or is this a theory question
at the micro-level where each comparison and change of a bit in
the implimentation of the 'addition' operation is to be counted?
Is this an assignment in designing an IC which is faster for these
particular cases than building a full-blown adder circuit would be?

--
Reviewers should be required to produce a certain number of
negative reviews - like police given quotas for handing out
speeding tickets. -- The Audio Anarchist
Jul 23 '05 #8
Alex Vinokur wrote:
Consider the following statement:

n + i, where i = 1 or 0.

Is there more fast method for computing n + i than direct computing that sum?


No.
But a good optimizing compiler should be able to
replace n + 0 with n and replace n + 1 with ++n.
Jul 23 '05 #9
"Walter Roberson" <ro******@ibd.n rc-cnrc.gc.ca> wrote in message news:cv******** **@canopus.cc.u manitoba.ca...
In article <37************ *@individual.ne t>,
Alex Vinokur <al****@big-foot.com> wrote:
:Consider the following statement:
:n+i, where i = 1 or 0.

:Is there more fast method for computing n+i than direct computing that sum?

It depends on the costs you assign to the various operations -- a
matter which is architecture dependant. Integer addition is usually one of
the fastest things a computer does. Suppose you were able to find a
two instruction sequence that was faster for that particular case: then
it is very likely to be slower because internally the CPU has
to perform an integer addition in order to find the address of the
second instruction.

Have you perhaps omitted some important facts about the circumstances?
For example, are you microprogrammin g, or is this a theory question
at the micro-level where each comparison and change of a bit in
the implimentation of the 'addition' operation is to be counted?
Is this an assignment in designing an IC which is faster for these
particular cases than building a full-blown adder circuit would be?


I would like to optimize (speed) an algorithm for computing very large Fibonacci numbers using the primary recursive formula.
The algorithm can be seen at
http://groups-beta.google.com/group/...e76b12150613a1

Function AddUnits() contains a line
n1 += (n2 + carry_s); // carry_s == 0 or 1

The question is if is it possible to make that line to work faster?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Jul 23 '05 #10

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

Similar topics

3
2195
by: Daragoth | last post by:
Hi, I'm writing a program using Metrowerks CodeWarrior 4.0 that determines the best possible combination of a data set by checking every possible combination. I found there were about 250,000,000 possibilities and decided to insert a cout in the loop that gave the percentage that had been completed. The program worked, but I soon realized it would take almost a month of execution time for it to finish. I figured the cout was slowing the...
7
1740
by: drewnoakes | last post by:
I have an application that performs custom deserialisation of object state from byte arrays. This happens very regularly, so needs to be fast. In addition, most of the strings repeat, meaning I'm deserialising the same sequence of bytes repeatedly, giving the same output string. Let's ignore the text encoding method, as it's not relevant to my question. Right now, I'm using BinaryReader.ReadString() which gives the correct result,...
20
9142
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
23
510
by: Alex Vinokur | last post by:
Consider the following statement: n+i, where i = 1 or 0. Is there more fast method for computing n+i than direct computing that sum? -- Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html
6
3402
by: thecodemachine | last post by:
Hi, I'm looking for a fast and simple one to one hash function, suitable for longer strings (up to 2048 in length). I'd like keys to be relatively short, I doubt I'd be creating more than 256 keys.. I could use md5 but even that is more than I need, and I assume not the fastest algorithm (?), so I'm reluctant to use it. I've been looking around a lot, not finding much anything come to mind?
0
1291
by: Dancorbier | last post by:
uCalc Fast Math Parser 2.95 is now available. This component allows your applications to evaluate math expressions defined at runtime. The two major new enhancements in this version are faster speed, and more direct support for .NET. The previous version supported VB.NET with an include file, which didn't work directly with C#. Now there's a special .NET DLL file, which is designed to work with any .NET compiler. A full C# demo...
0
8357
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8277
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8700
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.