473,791 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how not use memmove when insert a object in the list

Hi :

python list object like a stl vector, if insert a object in the front or the middle of it,
all the object after the insert point need to move backward.

look at this code ( in python 2.4.3)

static int
ins1(PyListObje ct *self, int where, PyObject *v)
{
int i, n = self->ob_size;
PyObject **items;
if (v == NULL) {
PyErr_BadIntern alCall();
return -1;
}
if (n == INT_MAX) {
PyErr_SetString (PyExc_Overflow Error,
"cannot add more objects to list");
return -1;
}

if (list_resize(se lf, n+1) == -1)
return -1;

if (where < 0) {
where += n;
if (where < 0)
where = 0;
}
if (where > n)
where = n;
items = self->ob_item;
for (i = n; --i >= where; ) /// here, why not use memmove? it would be more speed then this loop.
items[i+1] = items[i];
Py_INCREF(v);
items[where] = v;
return 0;
}

Kyo.

Apr 30 '06 #1
1 1530
On 30/04/2006 11:57 AM, kyo guan wrote:
Hi :

python list object like a stl vector, if insert a object in the front or the middle of it,
all the object after the insert point need to move backward.

look at this code ( in python 2.4.3)
for (i = n; --i >= where; ) /// here, why not use memmove? it would be more speed then this loop.
items[i+1] = items[i];


Here's a guess, based on similar work on another language a few
reincarnations ago :-)

memmove() is very general-purpose, and starts with byte addresses and a
byte count. For a small number of list elements, by the time that
memmove has determined (1) the move overlaps (2) both source and target
are on word boundaries and it is moving a whole number of words (3) what
direction (up or down), the DIY code has already finished. For a large
number of items, memmove *may* be faster (depending on the architecture
and the compiler) but you are using the wrong data structure anyway.
Apr 30 '06 #2

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

Similar topics

21
5272
by: Method Man | last post by:
Just a few theoretical questions I had on 'memmove': 1. Is there ever a good reason to use memcpy instead of memmove? 2. Is memmove useful to copy structs (as opposed to = operator)? 3. In general, when should memmove explicitly be used?
6
6093
by: novice | last post by:
Please explain with an example whts the DIFFERENCE between "memcpy" and "memmove"
5
1394
by: kpp9c | last post by:
I have a several list of songs that i pick from, lets, say that there are 10 songs in each list and there are 2 lists. For a time i pick from my songs, but i only play a few of the songs in that list... now my wife, Jessica Alba, comes home, and i start playing from Jessica's list of songs. After playing a few songs, Jessica, who needs her beauty sleep, goes to bed, and i start my play loop which starts picking from my songs again... ...
14
2806
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> #define SIZE_OF_DST 3 int main(void)
0
9669
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
10427
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
10207
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
10155
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
7537
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
6776
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
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
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.