473,804 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

No built-in swap function?

I am looking for the most effective way to exchange v[i] and v[j], and
expected to find something like

v.swap(i, j)

but it doesn't seem to exist. Is there anything better than

v[i], v[j] = v[j], v[i]

(which I don't like particularly because it evaluates each of v[i] and v[j]
twice)?
Jul 18 '05 #1
3 3469
"Andrew Koenig" <ar*@acm.org> writes:
I am looking for the most effective way to exchange v[i] and v[j], and
expected to find something like

v.swap(i, j)

but it doesn't seem to exist. Is there anything better than

v[i], v[j] = v[j], v[i]

(which I don't like particularly because it evaluates each of v[i] and v[j]
twice)?


I don't think so.

It seems an odd question in one way: Saying that it evaluates v[i] and
v[j] twice may be true, but ignores the fact that v[a] is quite
different from v[a] = b -- one does a lookup (whether it be a dict
lookup, list indexing operation, or whatever), and the other is an
assignment (whether setting a dict key/val pair, setting a list item,
etc).

I admit I don't like the repetition of v[i] and v[j], though of course
that can be solved by writing a function. I suppose a function is
best here, not a method, because you might want to apply it to any
sequence.

I-am-not-a-language-lawyer-ly y'rs,
John
Jul 18 '05 #2
"John J. Lee" <jj*@pobox.co m> wrote in message
news:ad******** **@pobox.com...
It seems an odd question in one way: Saying that it evaluates v[i] and
v[j] twice may be true, but ignores the fact that v[a] is quite
different from v[a] = b -- one does a lookup (whether it be a dict
lookup, list indexing operation, or whatever), and the other is an
assignment (whether setting a dict key/val pair, setting a list item,
etc).
Of course. Nevertheless there are some operations in common, especially if
you're trying to do something like

v[i], v[i+j] = v[i+j], v[i]
I admit I don't like the repetition of v[i] and v[j], though of course
that can be solved by writing a function. I suppose a function is
best here, not a method, because you might want to apply it to any
sequence.


Perhaps. Obviously, I can write

def swap(v, i, j):
v[i], v[j] = v[j], v[i]

and then my example above becomes

swap(v, i, i+j)

However, if swap were a builtin, I think I'd expect it to be a method
because that way, it would be easier for the function to depend on the type
of v.

Anyway, it's no big deal; I just want to be sure I wasn't missing something
that's already there.
Jul 18 '05 #3

"Simon Wittber" <dr******@metap lay.com.au> wrote in message
news:000201c427 64$5f43d3b0$000 0fea9@simonxp.. .
And received the following output:
1 million tuple swaps in 1.78324228359 seconds.
1 million temp swaps in 1.53032270861 seconds.
1 million xor swaps in 2.17933312753 seconds.

Interestingly, using psyco.full() produced these results:
1 million tuple swaps in 3.06005958858 seconds.
1 million temp swaps in 3.01621882111 seconds.
1 million xor swaps in 3.03376686143 seconds.


I *suspect* that what this means is that Psyco adds about 50% to function
call overhead due checking types to determine which specilized
machine-coded version to call. With any substantial optimizable
computation done within the function, this is still a net win, but not when
the function body is trivial.

Terry J. Reedy


Jul 18 '05 #4

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

Similar topics

1
2273
by: JimmyT | last post by:
I just configured and installed 2.3.4 and noticed there is no datetime module. I noticed there is a datetimemodule.c file that did not get built (ie no object file). Is there something I need to do to my system to make this module? Thanks, Jim
1
3121
by: Alex Elbert | last post by:
Hi I have built dynamic HTMLTable. Now I want to attach it directly to the Email Body - it is already built, so why not to use a ready table. However, I cannot find the way of getting plain HTML text out of dynamically built control. I tried to put my table between div and read div.innerHTML then - HTTP exception has been thrown. Any thoughts, ladies and gentelmen
0
2193
by: Andrew Crook | last post by:
does MYSQL have a quota built into it! I need it limit the size of each database AndiC
1
1894
by: Mark | last post by:
Is there a way to execute a statement that is built dynamically by a .NET application. For example I have a loop that is reading values from a database and I want to do something like the following. Dim stringa as string = "response.write somavalue" Execute(stringa) I realize this is not the best way to do this but it is
4
6590
by: Yasutaka Ito | last post by:
Hi, Is there a way to determine which version of .NET Framework any given assembly is built with? thanks! -Yasutaka
1
2111
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
1
2184
by: William | last post by:
Looking for a pre built dot net website for consulting business. I am trying to put up a quick business web for a dot net frame work. I have a provider already. I am trying to save time. Any ideas will be appreciated. I will look at anything that can be modified VS 2003.
1
1550
by: Daniel | last post by:
is there any way to get to a unique build verion of an assembly at runtime? e.g. a version that is unique to the time that the assembly was built?
48
4964
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott David Daniels where he said that probably the VS2003 toolkit will be used for Python 2.5 again. However, even before the release of Python 2.5, I cannot seem to find many retailers around here that still carry Visual Studio 2003, and some were a...
3
1795
by: drewj840 | last post by:
I built a Windows service that sweeps a set of folders every 60 seconds and puts the files into a SQL Server database. I am creating a second service that will delete this set of folders and recreate them each night based on a list of active customers. I need to be able to stop the first service before starting the second service and then when the second service completes restart the first service. I haven't the slightest idea how to proceed.
0
9594
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
10346
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
10347
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
7635
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
6863
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
5531
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
3832
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.