473,748 Members | 5,242 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 #1
27 2348
mike3 <mi******@yahoo .comwrote:
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
Did you turn on optimization? In some systems vector does a whole bunch
of error checking unless full optimizations have been turned on.
Jan 24 '08 #2
On Jan 24, 5:41 am, "Daniel T." <danie...@earth link.netwrote:
mike3 <mike4...@yahoo .comwrote:
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

Did you turn on optimization? In some systems vector does a whole bunch
of error checking unless full optimizations have been turned on.
Yes, I did. It doesn't seem to be the vector -- the out-of-place
routine
uses vectors as well, and it performs twice as fast as the in-place
one,
at least on my machine. It seems to have to do with that stack
thingy.
Jan 24 '08 #3
In article <1d5185f0-b105-4201-9c52-
b2**********@v4 6g2000hsv.googl egroups.com>, mi******@yahoo. com says...

[ ... ]
Yes, I did. It doesn't seem to be the vector -- the out-of-place
routine uses vectors as well, and it performs twice as fast as the
in-place one, at least on my machine. It seems to have to do with
that stack thingy.
It would help tremendously if you posted (here or to the web site) code
that could be compiled and included (at least) the routine with which
you're having a problem.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 25 '08 #4
On Jan 24, 9:15*pm, Jerry Coffin <jcof...@taeus. comwrote:
In article <1d5185f0-b105-4201-9c52-
b2d03c0c0...@v4 6g2000hsv.googl egroups.com>, mike4...@yahoo. com says...

[ ... ]
Yes, I did. It doesn't seem to be the vector -- the out-of-place
routine uses vectors as well, and it performs twice as fast as the
in-place one, at least on my machine. It seems to have to do with
that stack thingy.

It would help tremendously if you posted (here or to the web site) code
that could be compiled and included (at least) the routine with which
you're having a problem.
Here's a version that will compile and run on a Windows
system with Microsoft's Visual C++ compiler (it can be
easily modified to compile and run on other systems, by
the way):

http://www.mediafire.com/?cznivxxynnr

And it has tests added in that run the routines in
question. The results on my machine were:

---
Test 1: 100M out-of-place multiplications @ 128 bits...done.
CPU time: 14.078 second(s).
Wall time: 14 second(s).
Final result: d08f168e 0b3a70a4 edb740a8 321bd2f1
Expected: d08f168e 0b3a70a4 edb740a8 321bd2f1

Test 2: 100M in-place multiplications @ 128 bits...done.
CPU time: 29.547 second(s).
Wall time: 30 second(s).
Final result: 39adced5 f7724919 3d983ab3 358522c1
Expected: 39adced5 f7724919 3d983ab3 358522c1
---
Jan 25 '08 #5
In article <74ca36c7-30fc-4d42-86ca-0e4f243161a6
@n22g2000prh.go oglegroups.com> , mi******@yahoo. com says...

[ ... ]
That's what the point of the vector stack was: to avoid having to keep
creating vectors. It would set up a pile of them that could then be
popped off as needed. Once there's enough for all the threads, no
more are created. They're just pushed/popped on/off the stack.
The vector stack probably isn't doing much good. In fact, it's probably
harmful.

The problem is that vector (like other standard containers) is value-
based. When you push something on the stack, it does NOT put the
original item on the stack. Rather, it creates a _copy_ of the value on
the stack. Likewise, when you pop something off of the stack, what you
get is a copy of what was on the stack. After the copy is made, the one
what was on the stack gets destroyed.

IOW, when you finish using a vector, you really ARE destroying it (just
what you wanted to avoid) AND you're making a copy of its (now mostly
useless) contents. When you need a vector again, you don't just create
an empty one and use it -- instead, create a copy of an existing on with
useless contents, then destroy that existing useless one, then (more
likely than not) resize its content to fit what you currently need.

C++ 0x will allow you to get what you want -- it has rvalue references
and move constructors, that allow you to actually move the original
object when you put it on the stack and again when you pop it off the
stack.

For now, you can probably get (sort of) the same effect by using some
sort of reference-counted wrapper, to avoid creating and destroying
vectors every time you push/pop them. Right now, I'd guess your attempt
at optimization is really a pessimization; this modification should at
least get you back to neutral (i.e. it should at least be as fast as
really simple-minded code would have been).

Looking over your code, I'd also advise restructuring your stack a bit.
Specifically, instead of sharing a single stack between all threads, I'd
create a separate stack for each thread. Profiling your code, it's
currently spending about 10% of its time entering and leaving critical
sections, which would be avoided by creating a stack per thread.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 26 '08 #6

"mike3" <mi******@yahoo .comwrote in message news:a0******** *************** ***********@i12 g2000prf.google groups.com...
On Jan 26, 3:00 pm, "John Scheldroup" <johnscheldr... @hush.comwrote:
"mike3" <mike4...@yahoo .comwrote in messagenews:c3* *************** *************** ***@s19g2000prg .googlegroups.c om...
On Jan 26, 12:39 pm, "John Scheldroup" <johnscheldr... @hush.com>
wrote:
"mike3" <mike4...@yahoo .comwrote in messagenews:40* *************** *************** ***@d70g2000hsb .googlegroups.c om...
On Jan 26, 12:15 am, Jerry Coffin <jcof...@taeus. comwrote:
In article <74ca36c7-30fc-4d42-86ca-0e4f243161a6
@n22g2000prh.go oglegroups.com> , mike4...@yahoo. com says...
[ ... ]
That's what the point of the vector stack was: to avoid having to keep
creating vectors. It would set up a pile of them that could then be
popped off as needed. Once there's enough for all the threads, no
more are created. They're just pushed/popped on/off the stack.
The vector stack probably isn't doing much good. In fact, it's probably
harmful.
If you notice how it's coded, the stack contains a list of _pointers_
to preallocated vectors.
Would there be a Template parameter defined in its list to see
what types are preallocated. The compiler can preinstall the
type first before the stack allocates. I could be wrong but the other
way *what type of parameter* do we have before FGWDStack()
initializes. Templates.. those curious creatures...
<snip>
I've noticed you got rid of the vector of _pointers_ and replaced it
with a vector of objects. But then you run into all those problems
with copying objects -- why not keep the vector of _pointers_?

If objects are being copied they are not getting reused,
likely such the result of to many details to early on
>Like what, exactly? What sort of details would that be?
Its a template not an algorithm
>><snip>
>Does all that stuff like hiding pointers impact the
performance any? Remember, I need every last ounce of
performance I can get out of this.
I don't recall your saying such.. why use templates
anyhow ? If not what kind are the reusability issues
that lead you to employ them ?

John

Jan 27 '08 #7
On Jan 26, 5:59*pm, "John Scheldroup" <johnscheldr... @hush.comwrote:
"mike3" <mike4...@yahoo .comwrote in messagenews:a0* *************** *************** ***@i12g2000prf .googlegroups.c om...

On Jan 26, 3:00 pm, "John Scheldroup" <johnscheldr... @hush.comwrote:


"mike3" <mike4...@yahoo .comwrote in messagenews:c3* *************** *************** ***@s19g2000prg .googlegroups.c om...
On Jan 26, 12:39 pm, "John Scheldroup" <johnscheldr... @hush.com>
wrote:
>"mike3" <mike4...@yahoo .comwrote in messagenews:40* *************** *************** ***@d70g2000hsb .googlegroups.c om...
On Jan 26, 12:15 am, Jerry Coffin <jcof...@taeus. comwrote:
>In article <74ca36c7-30fc-4d42-86ca-0e4f243161a6
>@n22g2000prh.g ooglegroups.com >, mike4...@yahoo. com says...
>[ ... ]
That's what the point of the vector stack was: to avoid having to keep
creating vectors. It would set up a pile of them that could thenbe
popped off as needed. Once there's enough for all the threads, no
more are created. They're just pushed/popped on/off the stack.
>The vector stack probably isn't doing much good. In fact, it's probably
>harmful.
If you notice how it's coded, the stack contains a list of _pointers_
to preallocated vectors.
>Would there be a Template parameter defined in its list to see
>what types are preallocated. The compiler can preinstall the
>type first before the stack allocates. I could be wrong but the other
>way *what type of parameter* do we have before FGWDStack()
>initializes. Templates.. those curious creatures...
<snip>
I've noticed you got rid of the vector of _pointers_ and replaced it
with a vector of objects. But then you run into all those problems
with copying objects -- why not keep the vector of _pointers_?
If objects are being copied they are not getting reused,
likely such the result of to many details to early on
Like what, exactly? What sort of details would that be?

Its a template not an algorithm
><snip>
Does all that stuff like hiding pointers impact the
performance any? Remember, I need every last ounce of
performance I can get out of this.

I don't recall your saying such.. *why use templates
anyhow ? If not what kind are the reusability issues
that lead you to employ them ?
I'm using templates because I might need stacks of other
types of buffers beside vectors of bignum digits.

Jan 27 '08 #8
In article <MP************ ************@ne ws.sunsite.dk>,
jc*****@taeus.c om says...

[ ... ]
Func Func+Child Hit
Time % Time % Count Function
---------------------------------------------------------
380.550 5.3 7165.489 100.0 1 _main (rawdigit.obj)

This says that main executed for 380.550 milliseconds, which was 5.3
percent of the total time taken. Along with the other functions it
called, it took 7165.489 milliseconds, which was 100% of the total time
(as you'd expect for main). The name in the object file is _main, and
the object file it came from is rawdigit.obj.
Oops, sorry. I skipped over the hit count -- that's the number of times
the profiler saw this function called.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 27 '08 #9
In article <5be30f55-e321-4ad8-9890-e2930e973674
@s27g2000prg.go oglegroups.com> , mi******@yahoo. com says...

[ ... ]
Hmm. This makes a lot more sense and would be
quite a bit better than the stack -- it wouldn't
involve that mutex in there either.
I've finally had a chance to look at the code a bit more. I was looking
at MulInPlace:

void RawDigit::MulIn Place(const RawDigit &a)
{
/* Obtain a temporary buffer to hold the result. */
std::auto_ptr<R awDigittmpBuf(r dStack.Pop());
RawDigit *pTmpBuf=tmpBuf .get();

/* Make it big enough if it isn't already. */
pTmpBuf->Resize(lengt h + a.length);

/* Do multiplication */
pTmpBuf->Mul(*this, a);

/* Copy result back */
Copy(*pTmpBuf);

/* Done! Save the buffer for future reuse. */
rdStack.Push(tm pBuf);
}

Since you don't need pTmpBuf's value after you copy it into the current
object, you can probably save a fair amount of time by swapping the two
vectors instead. Copying has linear complexity but swapping is constant
complexity.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 27 '08 #10

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

Similar topics

11
2442
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
1477
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
2983
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
2206
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
8987
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
9534
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...
1
9316
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
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
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...
0
6073
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
4597
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
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
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

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.