473,796 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fast list traversal

I want to see if there is an alternative method for fast list
traversal. The code is very simple:

dict_long_lists = defaultdict(lis t)
for long_list in dict_long_lists .itervalues()
for element in long_list:
array_a[element] = m + n + p # m,n,p
are variable numbers

The long_list's are read from a defaultdict(lis t) dictionary and so
don't need initializing. The elements of long_list are integers and
ordered (sorted before placing in dictionary). There are 20,000
long_list's each with a variable number of elements (>5,000). The
elements of long_list are immutable (ie. don't change). The above
code is within a def function.

I've tried set() using defaultdict(set ) but the elements are not
ordered.

What is the fastest way to traverse these long_list's sequentially
from the beginning to the end? Maybe there is another data structure
that can be used instead of a list.

Dinesh
Nov 2 '08 #1
4 2434
On Nov 2, 1:00*am, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
On Sun, 2 Nov 2008 00:25:13 -0700 (PDT), dineshv
<dineshbvad...@ hotmail.comdecl aimed the following in comp.lang.pytho n:
I want to see if there is an alternative method for fast list
traversal. *The code is very simple:
dict_long_lists = defaultdict(lis t)
for long_list in dict_long_lists .itervalues()
* * * * for element in long_list:
* * * * * * * * array_a[element] = m + n + p * * * * * * * *# m,n,p
are variable numbers
The long_list's are read from a defaultdict(lis t) dictionary and so
don't need initializing. *The elements of long_list are integers and
ordered (sorted before placing in dictionary). *There are 20,000

* * * * Out of curiosity, what code is used to put the values in?The sample
you give above is creating an empty dictionary rigged, if I understand
the help file, to automatically give an empty list if a non-existent key
is requested. But in your loop, there is no possibility of a
non-existent key being requested -- .itervalues() will only traverse
over real data (ie; keys that DO exist in the dictionary).

* * * * And, if you are sorting a list "before placing in dictionary", why
need the defaultdict()? A plain

* * * * dict[key] = presorted_list_ of_integers

would be sufficient.

* * * * Or do you mean to imply that you are using something like:

* * * * thedefaultdict[key].append(single_ value)
* * * * thedefaultdict[key].sort()

EACH time you obtain another value from where-ever? If so, that's going
to be the biggest time sink...
What is the fastest way to traverse these long_list's sequentially
from the beginning to the end? *Maybe there is another data structure
that can be used instead of a list.

* * * * So far as I know, the list IS the fastest structure available for
sequential processing.
--
* * * * Wulfraed * * * *Dennis Lee Bieber * * * ** * * KD6MOG
* * * * wlfr...@ix.netc om.com * * * * * * *wulfr...@besti aria.com
* * * * * * * * HTTP://wlfraed.home.netcom.com/
* * * * (Bestiaria Support Staff: * * * * * * * web-a...@bestiaria. com)
* * * * * * * * HTTP://www.bestiaria.com/
dict_long_lists is a dictionary of lists and is NOT empty. Thank-you
Nov 2 '08 #2
dineshv:
What is the fastest way to traverse these long_list's sequentially
from the beginning to the end? *Maybe there is another data structure
that can be used instead of a list.
Psyco can help a lot in that kind of code.

>The elements of long_list are immutable (ie. don't change).<
A tuple too may be fit then, but probably it doesn't improve the
situation.

Bye,
bearophile
Nov 2 '08 #3
On Sun, 02 Nov 2008 00:25:13 -0700, dineshv wrote:
I want to see if there is an alternative method for fast list traversal.
The code is very simple:

dict_long_lists = defaultdict(lis t)
for long_list in dict_long_lists .itervalues()
for element in long_list:
array_a[element] = m + n + p # m,n,p
are variable numbers

It might help if you showed some sample data, because your explanation is
confusing. You are asking about traversing lists, but your data is in a
defaultdict. Why is it in a dict, when you don't seem to be using the key
anywhere? Putting that aside, I'm going to make a guess and assume your
data looks something like this...

dict_long_lists = {
'key1': [0, 1, 2, 3, 4, 5],
'key2': [16, 17, 18, 19],
'key3': [7, 9, 11, 13, 15],
'key4': [6, 8, 10, 12, 14] }

Then you do something like this:

array_a = [None]*20

Then after running your code, you end up with:

array_a == [x0, x1, x2, x3, .... , x19]
where each x is calculated from m + n + p.
The long_list's are read from a defaultdict(lis t) dictionary and so
don't need initializing. The elements of long_list are integers and
ordered (sorted before placing in dictionary). There are 20,000
long_list's each with a variable number of elements (>5,000). The
elements of long_list are immutable (ie. don't change). The above code
is within a def function.

I've tried set() using defaultdict(set ) but the elements are not
ordered.
It's not clear what you have tried to do with set(), or why the elements
need to be ordered.

What is the fastest way to traverse these long_list's sequentially from
the beginning to the end? Maybe there is another data structure that
can be used instead of a list.
I doubt you'll find anything faster than a list.

You have 20,000 lists of 5,000 items each, which means you have a
*minimum* of 100,000,000 items to traverse. If each iteration takes 0.1
millisecond, not an unreasonably slow speed depending on the amount of
computation each iteration is, that will take 10,000 seconds or nearly
three hours. The only solutions to that are to reduce the amount of
computation in each loop, reduce the number of items, or get a faster
computer.

Have you tried running the profiler to see where the time is actually
going? I suggest you write a small set of test data (say, 1000 items),
and profile it, then write a longer set of test data (say, 100,000
items), and if it's still not clear where the time is being lost, ask for
help.
--
Steven
Nov 2 '08 #4
Steven D'Aprano:
The only solutions to that are to reduce the amount of
computation in each loop, reduce the number of items, or get a faster
computer.
Changing language too is an option :-)
Languages like Java, D, C, C++ may help :-)

Bye,
bearophile
Nov 2 '08 #5

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

Similar topics

3
5530
by: Robert Oschler | last post by:
What's a good way to find a specific text node element in a web page's DOM tree? I thought of traversing each node but there has to be a faster way. Is there a "find text node by nodeValue" function or something? I do know the nodeValue (text string) of the text node. Thanks.
4
2932
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows traversal only in one direction, I would say that using appropriate loops/recursion, traversal in opposite direction is also possible. Then why the need for doubly linked list? --
4
9830
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
14
2247
by: Karch | last post by:
I need to find the fastest way in terms of storage and searching to determine if a given string contains one of a member of a list of strings. So, think of it in terms of this: I have a string such as the following: "Smith said the dog belongs (or did belong) to the secretary, an employee of the company." Then the list contains the following (and this list is MUCH larger in the real situation):
0
9680
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
9528
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
10456
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
10174
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
9052
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
7548
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
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
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.