473,763 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do I really have to use an array?

Hi.

I can't believe I may have to use an array here.

I've got this bignum package I was making in C++ for a fractal
generator, and tried an approach that was suggested to me here a while
back, about using a "stack of vectors" instead of a static array.

Anyway, I put the suggestion into effect, and it seems the routine
that uses the stack of vectors (an in-place multiplication routine) is
real slow -- too slow for my liking. The out-of-place multiplication
is like twice as fast. What gives? Do I really have to use an evil
array on the stack? (in my code it would be something like "Digit
tmpBuf[2*MAX_PRECISION]".) I don't think so since it was said the
stack-of-vectors approach can be at least as fast as the array.

You can get the relevant code snippets here if you need to see them:
http://www.mediafire.com/?51qszh1cv2j
Jan 24 '08
27 2351
On Feb 5, 7:27 am, "Ralph D. Ungermann" <use...@mloge-ungermann.de>
wrote:
<snip>
for ( std::size_t i = myLength; i--; )
{
//...

}
So that actually stops after "i" goes to 0?
Feb 5 '08 #21
On Feb 5, 3:27 pm, "Ralph D. Ungermann" <use...@mloge-ungermann.de>
wrote:
mike3 wrote:
I was using int instead of std::size_t becuase loops like
this, which are used in several routines, do not work:
std::size_t myLength(myVect or.size());
for(std::size_t i(myLength-1);i>=0;i--)
{
... do something ...
}
Or is there a way to still use std::size_t without these
conversions???
for ( std::size_t i = myLength; i--; )
{
//...
}
Nice obfuscation.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 6 '08 #22
"Alf P. Steinbach" <al***@start.no wrote in message
news:13******** *****@corp.supe rnews.com...
If you want the range myLenght-1 downto 0, inclusive, consider just
for( size_t i = myLength-1; i != size_t(-1); --i )
Cheers, & hth.,
I have to say that this example is too clever for my taste. I'd rather
write:

for (size_t i = myLength; i != 0; ) {
--i;

// whatever

}

One reason for my preference is that this technique works for bidirectional
iterators too:

for (list<T>::itera tor it = myList.end(); it != myList.begin(); ) {
--it;

// whatever

}

whereas the "myLength-1" technique doesn't.
Feb 6 '08 #23
In message
<f1************ *************** *******@j78g200 0hsd.googlegrou ps.com>,
James Kanze <ja*********@gm ail.comwrites
>On Feb 5, 3:27 pm, "Ralph D. Ungermann" <use...@mloge-ungermann.de>
wrote:
>mike3 wrote:
I was using int instead of std::size_t becuase loops like
this, which are used in several routines, do not work:
std::size_t myLength(myVect or.size());
for(std::size_t i(myLength-1);i>=0;i--)
{
... do something ...
}
Or is there a way to still use std::size_t without these
conversions???
>for ( std::size_t i = myLength; i--; )
{
//...
}

Nice obfuscation.
That's another word for "idiom", isn't it? Guaranteed termination, loop
invariant exists throughout the block, ... what more do you want?

(And it doesn't use break ;-)

--
Richard Herring
Feb 6 '08 #24
On Feb 6, 6:34 pm, Richard Herring <ju**@[127.0.0.1]wrote:
In message
<f12082d8-8b8f-46c1-9505-e4e852687...@j7 8g2000hsd.googl egroups.com>,
James Kanze <james.ka...@gm ail.comwrites
for ( std::size_t i = myLength; i--; )
{
//...
}
Nice obfuscation.
That's another word for "idiom", isn't it? Guaranteed
termination, loop invariant exists throughout the block, ...
what more do you want?
Readability.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 7 '08 #25
In message
<86************ *************** *******@e4g2000 hsg.googlegroup s.com>,
James Kanze <ja*********@gm ail.comwrites
>On Feb 6, 6:34 pm, Richard Herring <ju**@[127.0.0.1]wrote:
>In message
<f12082d8-8b8f-46c1-9505-e4e852687...@j7 8g2000hsd.googl egroups.com>,
James Kanze <james.ka...@gm ail.comwrites
>for ( std::size_t i = myLength; i--; )
{
//...
}
>Nice obfuscation.
>That's another word for "idiom", isn't it? Guaranteed
termination, loop invariant exists throughout the block, ...
what more do you want?

Readability.
If it's an idiom, you don't need to read it. Whether it's an idiom is
ultimately a question of style guides and/or familiarity. If you want to
say that all reverse-indexing (as opposed to hiding the reversal inside
reverse iterators) is inherently unidiomatic, I wouldn't argue.

--
Richard Herring
Feb 7 '08 #26
James Kanze wrote:
On Feb 5, 3:27 pm, "Ralph D. Ungermann" <use...@mloge-ungermann.de>
wrote:
>for ( std::size_t i = myLength; i--; )

Nice obfuscation.
The postfix `trick' is uncommon, but not unknown. After a first
irritation, one ought to get it -- and remember, if repeated.

Though the decrement is more correctly placed at the beginning of the
loop, this does neither enhance readability nor safety (IMHO).

I've seen too much code, where each loop has been handcrafted: mixing
int and size_t, cast both ways, decrementing i on-the-fly at the first
occurrance in the loop body, and worse. You'll agree, that this causes
much more obfuscation.

So I use my way, whenever I have to: using uncommon syntax for uncommon
loops.

But as you mention it, I'm willing to change my style to:

for ( std::size_t i = myLength; i--; /*backward*/ )
-- ralph
Feb 8 '08 #27
In message <fo**********@m urphy.mediascap e.de>, Ralph D. Ungermann
<us****@mloge-ungermann.dewri tes
>James Kanze wrote:
>On Feb 5, 3:27 pm, "Ralph D. Ungermann" <use...@mloge-ungermann.de>
wrote:
>>for ( std::size_t i = myLength; i--; )
Nice obfuscation.

The postfix `trick' is uncommon, but not unknown. After a first
irritation, one ought to get it -- and remember, if repeated.

Though the decrement is more correctly placed at the beginning of the
loop, this does neither enhance readability nor safety (IMHO).

I've seen too much code, where each loop has been handcrafted: mixing
int and size_t, cast both ways, decrementing i on-the-fly at the first
occurrance in the loop body, and worse. You'll agree, that this causes
much more obfuscation.

So I use my way, whenever I have to: using uncommon syntax for uncommon
loops.

But as you mention it, I'm willing to change my style to:

for ( std::size_t i = myLength; i--; /*backward*/ )
I was happy with this until yesterday, when a colleague pointed out that
the equivalent for iterators:

for (iterator i=container.end (); i-- != container.begin (); )
{
//...
}

doesn't work if the container is empty, whereas this is fine:

for (iterator i=container.end (); i != container.begin (); )
{
--i;
//...
}
--
Richard Herring
Feb 8 '08 #28

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

Similar topics

11
2444
by: Mr. Berserker | last post by:
I was posting stuff to a mailing list when a friend, Prof. Corbessero and I came up with this one. Perhaps you can help resolve this, or add anything else worth knowing?? Maybe it should be added to the FAQ for further reference... :) > > Ahh. This is my area... C stores arrays in a form called "column > major" order, which actually means the rows come first (don't ask!). > So, here is what a 3x4 array looks like in memory >
1
1479
by: John Smith | last post by:
I have a two dimentional char array. Before filling it using strtok(), I reset its elements to '\0' using two nested for loops. The code works as I hope it would but I wonder whether I really need to reset the array. The program would run faster if I don't need to reset. ------------------------------------------ int Array(void) .................
3
1963
by: michael | last post by:
Hi, I am trying to write an ASP.NET web app, in Visual Basic.NET, using Visual Studio.NET 2004, .NET framework 1.1.4322 SP1 Running the project/app on localhost while in dev/write/debug stage When I say "trying", I do have it written, and it works ... sort of, for some cases. The problems/issues?
1
1470
by: bruce | last post by:
hi... i have the following test python script.... i'm trying to figure out a couple of things... 1st.. how can i write the output of the "label" to an array, and then how i can select a given element of the array.. i know real basic.. 2nd.. where can i go to find methods of libxml2dom. i've been looking using google, but can't seem to find a site pointing out the underlying methods,
0
1831
by: P Pulkkinen | last post by:
Dear all, sorry, i know this code is far little too long to debug here, but there is really annoying logical error. If someone debugs this, I really offer warm virtual handshake. What this code SHOULD do: - read new (=updated) licensetext from file $license_path then - read and modify recursively all files from $current_dir, replacing old
56
2986
by: tasteless | last post by:
Hi guys, I need really hard questions (about 10) about PHP programming (some of elements OOP as well, but no MySQL questions - this is different part), this questions needs to be very hard, but the experienced senior PHP developer should answered on it. I've already searched in google and google groups archive but without any good results. So could anybody help me giving some link or sending some stuff to me ?
2
1399
by: DaTurk | last post by:
This is probably a very silly, and simple question. If I'm coding in CLI, and I want to copy an array to an array, not a deep copy, just something of the nature arr1 = arr2, what is going on? I assumed the address of the first element is getting copied, so i essentially have two handles to the same memory. And changes in one would be reflected in the other. But what if I have something like this
3
1536
by: a | last post by:
After several days struggling, I'm still unable to generate a string automatically *ONE by ONE*. I don't want to create a long array memorizing all the output strings and then traverse this array to print the content out. For example, for studying the frequency of 5-letter words composed of {a,b,c,d,e} in a string, the length of array of (aaaaa, aaaab, ...., caaaa, ..., eeeee) will then be 5*5*5*5*5.
1
2207
nine72
by: nine72 | last post by:
Ok, I am at a complete loss on this and have finally come to the XML Parsing Gods (and perhaps a PHP minor deity) for guidance… I will try my best to describe what I have going on… 1) I have 15 form pages, well over 500 potential fields, which are written in PHP. While most pages are one time entry forms, there are 5 that can be “recycled” as many times as needed. An example would be the Contacts Form. A user can give me 1 contact and move...
0
10145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
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
9938
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,...
1
7366
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
6642
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
5270
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
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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 we have to send another system
3
3523
muto222
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.