473,898 Members | 2,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fast linked list

Hi,

for my current application, I need a linked list in which I can access and
walk through at highest speed possible - adding/deleting/inserting elements
can be as slow as necessary. Now, which template out of the C/C++ standard
library should I use for this - std::list? Or is there a better
implementation out there?

thanks
Peter
Nov 17 '05
14 1529
"Peter Schmitz" <Pe**********@d iscussions.micr osoft.com> wrote in message
news:28******** *************** ***********@mic rosoft.com...
Hi,

for my current application, I need a linked list in which I can access and
walk through at highest speed possible - adding/deleting/inserting
elements can be as slow as necessary. Now, which template out of the C/C++
standard library should I use for this - std::list? Or is there a better
implementation out there?


If your elements are bigger than a 32-bit word, then vectors and linked
lists will have about the same performance. Vectors will only be noticeably
faster if the elements are small, they are stored directly [i.e., not
through a pointer] and the operation on the element while traversing the
vector/list is very simple. Otherwise, they are equivalent and you should
consider other factors, such as the speed of insertion/deletion.

S
Nov 17 '05 #11
"Mark Randall" <st****@rapierc om.freeserve.co .uk> wrote in message
news:uK******** ******@TK2MSFTN GP14.phx.gbl
However, in my experience allocating large chunks of memory and
copying the contents is a hidiously slow process compared to a linked
list.


Adding to the end of std::vector is an amortized constant operation.
While the memory allocation and copying is slow, the vector does not
grow its storage one element at a time. Instead, it grows by some factor
(e.g., doubles its storage on every reallocation). Thus, for every
expensive add operation, there are many cheap ones - adding an element
when there's still space in the buffer does not involve any memory
allocation or any copying (besides the element itself), so it's faster
than adding an element to linked list where a node must be allocated on
the heap.

The large cost of occasional memory reallocation is spread, or
amortized, across many cheap operations. That's what "amortized
constant" means.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Nov 17 '05 #12
If the O.P. doesn't know the maximum size it will ever get to or can't
determine it, then he could reallocate a whole new array -
"adding/deleting/inserting elements can be as slow as necessary" he said.
But it would be better to just put a finger on the maximum ever size at the
beginning. If the O.P. doesn't want to consume the maximum amount of memory
all of the time, then this is something he failed to point out.
"Mark Randall" <st****@rapierc om.freeserve.co .uk> wrote in message
news:uW******** ******@TK2MSFTN GP11.phx.gbl...
Ever tried adding things to a static array?...

- MR

"Bonj" <a@b.com> wrote in message news:32******** *****@individua l.net...
Why don't you just use an array?

"Peter Schmitz" <Pe**********@d iscussions.micr osoft.com> wrote in message
news:28******** *************** ***********@mic rosoft.com...
Hi,

for my current application, I need a linked list in which I can access
and
walk through at highest speed possible - adding/deleting/inserting
elements
can be as slow as necessary. Now, which template out of the C/C++
standard
library should I use for this - std::list? Or is there a better
implementation out there?

thanks
Peter



Nov 17 '05 #13
> for my current application, I need a linked list in which I can access and
walk through at highest speed possible -


Highly-optmized read side peformance... Is your list accessed by multiple
threads? If so, it seems that your asking for an algorithm that provides a
method to achieve lock-free reads. Here is a rock-solid algorithm that can
be implmented in assembly language:

http://www.research.ibm.com/people/m.../spaa-2002.pdf

This actually works, and its fairly straight forward.
Nov 17 '05 #14
Semi-OT:

I must admit that this is the first time I have EVER seen so many coders
discussing implimentations , code developments and comparisons for a single
topic.

Rather nifty in my opinion,

- Mark R

"Peter Schmitz" <Pe**********@d iscussions.micr osoft.com> wrote in message
news:28******** *************** ***********@mic rosoft.com...
Hi,

for my current application, I need a linked list in which I can access and
walk through at highest speed possible - adding/deleting/inserting
elements
can be as slow as necessary. Now, which template out of the C/C++ standard
library should I use for this - std::list? Or is there a better
implementation out there?

thanks
Peter

Nov 17 '05 #15

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

Similar topics

5
859
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that LOCATE subroutine that returns a node???? Any help would be appreciated. Thanks
8
2901
by: Neil | last post by:
I have a very puzzling situation with a database. It's an Access 2000 mdb with a SQL 7 back end, with forms bound using ODBC linked tables. At our remote location (accessed via a T1 line) the time it took to go to a record was very slow. The go to mechanism was a box that the user typed the index value into a combo box, with very simple code attached: with me.RecordsetClone .FindFirst " = " & me.cboGoTo If Not .NoMatch Then Me.Bookmark...
10
15144
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
6
4605
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any potential pitfalls I'd be extremely grateful. Cheers Steve
7
7433
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For example, the manner in which I update element 1 depends on several other (randomly numbered) elements in the array. So, I can't change an element until I've figured out how every element changes.
12
3968
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
19
5437
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and if you execute the latter, the size of the resulting lists cannot be known without explicitly counting the sizes of the new lists. I was thinking: What if size() was an O(n) operation only *after* a splice() operation has been performed (and...
0
8644
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
7
5784
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
9993
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
11254
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
10946
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
10480
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
9658
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
6076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4701
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
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3303
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.