Python is… slow?

One of the work items that kept me occupied over the past week was writing a computational physics simulation modelling “quantum potential wells using a Monte Carlo simulation”:http://www.phys.soton.ac.uk/teach/year3/notes/ph314/projects/pjtb2.pdf.

I chose to write the simulation in “Python”:http://www.python.org instead of C - my feeling was this would be quicker to get a working simulation with, and I could always rewrite in C if it was very slow. i didn’t expect it to be - I’d read a lot of reports about Scientists using Python, so figured it would fine. I also picked Python as this would force me to learn the language, something that if I pursue “a particular route”:http://www.ses.soton.ac.uk/projects/Comp_Eng_Des/comp_eng_des.html after graduating would be very useful.

Due to some problems, the lecturer overseeing this simulation ended up writing the simulation also, in the departmental version of Basic.

At that point I realised how slow the Python code was. I didn’t expect it to be as fast as C, but was surprised by how much faster the Basic version was. It wasn’t impossibly slow, but running a million random walks of up to 300 steps was taking nearly 2 hours to complete. I tried plugging in Psyco to speed up the Python code, without much luck.

I don’t know if there were ways to get the Python code faster (short of using Pyrex), but I’m posting the code here to see what people think. Given it was the first proper Python program I’ve done I expect it to be a bit slow, but wasn’t expecting it to be this much slower.

One other point that came out was that the Python code produced a greater scatter on the results than the Basic version. I think this has to come down to the random number generator, but thoughts on this would be appreciated as well.

To the code:

* “Python code for a 1 dimensional potential well”:/physics/simulations/rand_walks_qm/1D_0.py
* “New Forest Basic code for a 1 dimensional potential well”:/physics/simulations/rand_walks_qm/1D_0.nfb (slightly more basic than mine - walks aren’t killed after X steps, but continue to arrest). You’ll also need the editor/compiler/interpreter (”Windows”:http://www.phys.soton.ac.uk/teach/year2/notes/lab/software/nfbwin.exe or “Linux”:http://www.phys.soton.ac.uk/teach/year2/notes/lab/software/nfblnx) - which in turn requires “TCL”:http://www.tcl.tk/ to be installed.
* Python code for a 2 dimensional potential well (”ground state”:/physics/simulations/rand_walks_qm/2D_0.py , “first potential”:/physics/simulations/rand_walks_qm/2D_1.py , “second potential”:/physics/simulations/rand_walks_qm/2D_2.py ). Note that this code does not relate well to the real values expected; I didn’t work out what the problem was before I had to hand in the report.

I just ran across dirtSimple’s post “The perception of speed”:http://dirtsimple.org/2004/12/perception-of-speed.html today, so it seems I’ve written this post at a time when the subject’s topical :-)

Back again

Well, it’s been a very long time since I last updated this blog :-)
I’m into the third year of my degree now and dissertation work, amongst other things, has been taking up a lot of my time, leaving enough free time for relaxing but not much programming.

Now we’re into holiday season, I should hopefully have time to catch up with a few things.

Updating RSS entries in a database

I’m feeling a little brain-dead at the moment, so I’m throwing this question out.

I want to store items from a couple of feeds (all different varieties of RSS) in a database. What is the best way to make sure I don’t save the same entry more than once into the database?

Posting to PHP newsgroups with Opera

Have you managed to get “Opera’s”:http://www.opera.no/ news reader to post to the “PHP newsgroups”:nntp://news.php.net ? I have not been able to figure out what settings to use for the outgoing server - Opera implies my mail server, but this doesn’t seem to work.

I need to post to the PEAR list, but can’t subscribe to the email list and get so many messages :-)
Help!

Off on holiday

Back in just over a week. Don’t let the web collapse while I’m not around…

Wordpress, HTMLArea, and ‘Normal’ users

I’ve spent the past couple of days installing and tinkering with WordPress on
Mark’s site.
WordPress is a funny beast - its extremely popular, it does a good job, yet
has some horrible
code problems - like too many global variables causing namespace conflicts.

There’s some very strange ideas - for example, the prefixing of many files/directories
with wp-, yet leaving constants and global variables without any
prefix (Yes, I did have a couple of clashes when integrating into the site’s
templating
system). WordPress could really have done with more design work putting in
at the start (wp-blog-header.php anyone?),
but I guess that’s also why it’s so good: it’s the paradox that well designed
projects take ages to
get anywhere (or don’t get anywhere) while the others grow and become popular.
Not always, but I can think of a fair few like this :-)

The other thing that bugged me was the directory names are hard-coded in -
I wanted to rename the admin directory for security and to something more obvious
than wp-admin (why the prefix again…?). To do so would have required over
130 edits!

Anyhow, enough moaning about the code - it works, and does what I want, so
I’m satisfied (none of the better-coded tools I could find seemed as advanced)
- and I’m sure there’s a moral in there as I look on my hard drive at the third
redesign of a project that’s been around for 9 months and got no further than
the code architecture (look back through this blog for RSS
Manager
for earlier life).

The project in question was to give blogs to town councillors. Now councillors,
like 98% of the population, aren’t renowned for their computer skills, and
to make things easier I integrated HTMLArea into WordPress using Joe
Schmoe’s
WYSIWYG
Hack For Wordpress
(modified to work with HTMLArea 3.0RC1).

As these are supposed to be trusted people posting on the site, I’ve not bothered
with filtering the HTML produced, but that’s coming (unless I can find a script
someone else has written first!). But this is a digression…

One thing Mark wanted was for councillors to post images. An extensive search
turned up Wei Zhuo’s PHP Image
Manager, Image Editor for HTMLArea
.
This looked to be just thing thing, and also used my Image_Transform library (so it must be good :)).


The Image manager interface

Installing it was very simple. Feeling pleased with myself (look at how easy
I’ve made adding images to a website!!) I decided to demonstrate how simple
it was to my parents (Look Mum, even you can manage…). Two minutes and two
blank faces later, I was forced into the realisation that it still wasn’t simple
enough. The menu bar in HTMLArea itself was too complex (to be fair I’d installed
a lot of plugins) and they had no clue how to use the Insert Image dialogue
- despite it looking like a normal Windows Open File dialogue. When sat at
the computer, neither of them could work out how to upload an image.

Continue reading →

Visually editing PHP’s Docbook manuals

Having put off writing documentation for Image_Transform for too long (which I realised today when I came to write a program and couldn’t remember the API) I’ve been looking around at visual Docbook editors available for free. Here’s two I’ve found so far:

* “Morphon”:http://www.morphon.com/xmleditor/index.shtml
* “XMLmind Standard Edition”:http://www.xmlmind.com/xmleditor/stdedition.html

Both edit their own DocBook samples perfectly; neither will open PEAR’s documentation without errors.

Morphon says:
@FatalError at file file://localhost/C:/code/peardoc/en/preface.xml[line 18,column 27]: The entity “url.docbook.xml” was referenced, but not declared.@

XMLmind won’t even open the file, kicking up the error:
@parse error in “file:/C:/code/peardoc/en/preface.xml”, linke 18, column 9: reference to undefined entity “url.docbook.xml”@

How do I get these entities to be referenced? Obviously I’m missing something, but I’ve found nothing I understand in the documentation to fix the problem.

A text editor with memory?

Anyone know of a text editor that autosaves files even when they don’t exist on disk? I wrote a long post for this weblog this morning (and it was better than my usual posts), came back to the computer just now, and closed the text editor - clicking ‘No’ to the ‘Do you want to save changes?’ dialogue as well, without even bothering to look.

Hmmmph.

AOL 9 and old email

I’ve put a new hard drive into my parents’ computer, and installed AOL 9 on it. Previously they were using AOL 7, but wanted the spam filtering of version 9 (besides, 7 is starting to not work smoothly as AOL change their system).

The problem is that they have a few thousand emails in the Personal Filing Cabinets’ of AOL 7, and don’t want to lose these messages. I can’t find any way to get these emails into AOL 9. Is it possible? Google hasn’t been very helpful.

PHP Feature Request

With “people requesting”:http://www.procata.com/blog/archives/2004/07/29/goto-in-php/ @goto:@ statements for PHP, I thought it time I revealed the feature I’m wishing for :)
*I* want PHP to support…

p>. #ifdef … #endif preprocessor directives!

Why? Good question, let me explain my thinking…

I write a fair bit of debugging code in recent projects. Usually an @if (defined(’DEBUG’)@ statement containing @print@ statements; sometimes calls to a logging object/function (inside or without the @if@). None of which is needed when the code is in production.

Therefore if PHP included a preprocessor to remove these entries I could have faster execution, and save me having to run my files through a simple parser/regexp to remove such entries (OK, not done this yet, but I’ve been thinking of it).