Python ist krass

Momentan bin ich “gezwungen”, Software in Python zu schreiben. Und nach einigen Programmierer-Tagen bin ich immer mir noch nicht sicher, ob ich Python mag oder nicht. Einige Sachen sind cool, andere nicht und allzu häufig muss ich feststellen, dass Python ein riesiger Flickenteppich ist. (Allein schon, dass es “alte” und “neue” Klassen gibt – von der Syntax her – und dass diese nicht kompatibel sind, spricht Bände.)

Heute bin ich dafür mal wieder auf ein Konstrukt gestoßen, dass krass und cool zugleich ist:

for i in foo:

Es gibt hier also for-Schleifen mit else-Block. Und zwar wird der else-Block ausgeführt, wenn die Schleife komplett durchgelaufen ist (d.h. nicht durch ein break vorzeitig beendet wurde). Ziemlich praktisch.


Group box panel with GWT

I’m currently participating on a software development project using the Google Web Toolkit (GWT). While developing the GUI for a certain part of this project, I came across the need of a group box panel.

For those who don’t know what a group box is; here is an example:


Fortunately this type of panel is directly supported by HTML throught the <fieldset> tag. Unfortunately it isn’t supported by GWT (yet). However, it’s very simple to implement this using the SimplePanel class provided by GWT.


 * A group box panel (i.e. a frame where to which a caption can be
 * attached). The caption can be set via {@link #setCaption(String)}. 
 * The content can be set via {@link SimplePanel#setWidget()}.
 * @author Sebastian Krysmanski
public class GroupBoxPanel
  extends SimplePanel {

  private final Element m_caption = DOM.createLegend();
  public GroupBoxPanelImpl() {

    DOM.appendChild(getContainerElement(), this.m_caption);

  public GroupBoxPanelImpl(String caption) {

  public String getCaption() {
    return DOM.getInnerText(this.m_caption);

  public void setCaption(String caption) {
    DOM.setInnerText(this.m_caption, caption);

This code is released as public domain.

.NET – Array.Clear() vs. array[x] = 0 – Performance

So, yet another performance test for .NET. This time I was checking what’s the fastest way to clear an array (i.e. setting all array members to 0 or null).

The two contesters are:

  • array[x] = 0 (iterate over the array and simply set the values to 0)
  • Array.Clear()

Here are some results:

Clearing 5,000,000,000 items per test

Array.Clear() - Array size: 5 : 31.564s
Array[x] = 0  - Array size: 5 : 4.428s

Array.Clear() - Array size: 25 : 7.477s
Array[x] = 0  - Array size: 25 : 3.315s

Array.Clear() - Array size: 50 : 4.414s
Array[x] = 0  - Array size: 50 : 3.629s

Array.Clear() - Array size: 100 : 2.571s
Array[x] = 0  - Array size: 100 : 3.292s

Array.Clear() - Array size: 500 : 0.935s
Array[x] = 0  - Array size: 500 : 3.014s

Array.Clear() - Array size: 50000 : 0.621s
Array[x] = 0  - Array size: 50000 : 2.948s

In each test 5 billion int array items were cleared (that’s 20 GB). Tests was run first with a small array whose size was increased after each test run. The test were run as Release build.

As you can see:

  • For small arrays, array[x] = 0 is faster.
  • For large arrays, Array.Clear() is faster.
  • They’re about equally fast for array sizes between 50 and 100. (My guess is, somewhere around 75.)

And here’s the source code for this test:


.NET Locking Performance

Just a quick overview over the different lock types and their performance in .NET.

For this test, the following method was called as fast as possible for 4 seconds:

private void TestMethod() {
  lock (this) { // this locking is replaced depending on the locking type

Here are the results:

Locking Type Calls per second Factor
No locking (fastest possible) 470,972,276 19.61
Interlocked.CompareExchange 62,439,529 2.60
lock keyword 37,554,119 1.56
SpinLock (without owner tracking) 34,489,245 1.44
ReaderWriterLockSlim with LockRecursionPolicy.NoRecursion 25,214,451 1.05
ReaderWriterLockSlim with LockRecursionPolicy.SupportsRecursion 24,013,488 1.00

Full source code: Program.cs

SQLite Performance (RFC)

I’m currently working on a cross-platform SQLite .NET wrapper. At the moment it’s not really thread-safe. So, I was looking for ways of making it thread-safe.

Basically, there are two ways to do this:

  1. Share a single connection among all threads and use .NET locking mechanisms.
  2. Let each thread have its own connection (thus no .NET locking would be required).

To be able to make this decision, I did some performance tests and – assuming I did them right – got some interesting results you can read after the break.

The Setup

The tests ran on a Notebook featuring an Intel Core 2 Duo (2.53 GHz) and 8 GB memory. The OS was Windows 7 x64.

There are 40 tests in the suite testing various combinations of the available test parameters (see below).

Each test was executed for 1 to 20 threads to test concurrency.

Each test for a certain thread count (1, 2, 3, …) ran 30 seconds and was repeated 10 times.

Thus, the overall test duration was about 2 days.

In each test scenario, the SQLite database contained only one table with four columns. For SELECT tests this table was filled with 50,000 rows of random data.

There are two kinds of concurrent access:

  • Read access: Simulated by repeatedly selecting a random row from the table and reading all four values.
  • Write access: Simulated by inserting random values into the table.

The first batch of tests simulated read access, the second batch simulated write access, and the third batch simulated both concurrently.

Note: In all tests, the CPU was the limiting factor – not the hard drive.

Test Parameters

Each test comprises of a certain combination of the following test parameters:

  • Shared connection vs. multi connection: Whether all threads share the same database connection, or whether every thread has its own connection (to the same database though). Shared connections use SQLITE_OPEN_FULLMUTEX (serialized), multi connections use SQLITE_OPEN_NOMUTEX (multithread).
  • Read-only: Whether the connection is opened in read-only or read-write mode (SQLITE_OPEN_READONLY).
  • Shared cache: Whether all connections share the same cache (SQLITE_OPEN_SHAREDCACHE), or whether each connection has its own cache.
  • WAL: Whether the connection(s) use a database in WAL (write-ahead logging) journal mode.
  • Filled table: Whether the table to read from is empty or filled (not examined in this report due to missing data; I should mention though that trying to read from an empty table is significant slower than reading from a filled table).

Batch 1: read tests

Let’s start with the tests only reading data (i.e. no data is written during these tests). Each thread randomly reads a data row and then obtains all four values stored in it. This is repeated for 30 seconds.

select-statements.csv (file containing data for charts in this section)

Test: read-only

First test is about whether opening a database connection in read-only mode (SQLITE_OPEN_READONLY) does result in any performance benefit.

: read-write
: read-only
Shared connection - read-only: yes/no
Multi connection - read-only: yes/no

As you can see, there’s no benefit from choosing a read-only connection over a read-write connection (but it doesn’t hurt either).

Test: shared cache

Next, let’s check whether using a shared cache (SQLITE_OPEN_SHAREDCACHE) affects read performance.

, : no shared cache
, : use shared cache

For a shared connection (first chart) you can clearly see that using a shared cache is never better than using a private cache. The same is true for multiple connection (second chart).

Test: WAL

Next, we test the use of WAL (write-ahead logging). WAL is (suppose to be) bringing a performance benefit for concurrent write operations (which we don’t have here).

: no WAL
, : use WAL

As you can see, with few threads, using WAL for read operations results in a big performance boost (400% for shared connection, 700% for multi connections). However, when using a shared connection and more than 8 threads, WAL doesn’t provide any performance benefit anymore (but it also doesn’t hurt).

Summary: read tests

Let’s summarize what we’ve learned so far (for reading operations):

  • using a read-only connection doesn’t provide any performance benefit
  • using a shared cache is never faster (but sometimes slower) than using a private cache
  • using WAL is always faster than using the default journal mode (DELETE)

As for the question whether to use a shared connection or multiple connections, see this chart:

: one shared connection
: one connection per thread

This chart only contains the variations for shared and multi connections with the best performance, i.e. using WAL and no shared cache. As you can see, for very few threads (my guess: thread count <= cpu count), multiple connections perform much better. However, for more threads, a single shared connection is the better choice.

Batch 2: write tests

Next, let’s look at write-only tests. With these tests, multiple threads concurrently write to the same database table, inserting random data.

insert-statements.csv (file containing data for charts in this section)

Test: shared cache

Our first tests checks the performance for when a shared cache is used.

: private cache
: shared cache

As you can see, there’s no real difference between whether a shared cache or a private cache is used.

Test: WAL

Next, let’s check WAL (which improved read performance significantly even though it’s designed for write operations).

: no WAL
: use WAL

As expected, using a database in WAL mode drastically improves write performance.

Test: shared connections

The last thing to tests is whether to use multiple connections or a single shared one.

: shared connection
: one connection per thread

The results are clear. Using a shared connection always yields better write performance when using multiple threads.

Summary: write tests

To summarize the previous sections:

  • Using a shared cache doesn’t affect the performance.
  • Using WAL improves write performance significantly.
  • Using a shared connection is always faster than using multiple connections.


Batch 3: mixed reads and writes test

The last batch combines the previous two batches. This time the same number of read and write threads read and write concurrently from/to the same database table.

mixed-statements.csv (file containing data for charts in this section)

Assumption: WAL improves general performance

The previous tests clearly showed that enabling WAL improves read as well as write performance. Let’s check whether this is still true for concurrent reads and writes.

: no WAL
: use WAL

Again, enabling WAL results in a significant performance boost.

Note: Reading without WAL is extremely slow (under 1000 rows per second for 10 threads or less).

Test: Shared or multiple connections

Next, check whether we should use a shared connection or multiple connections.


As you can see, in both cases using one connection per threads and using WAL provides the best performance.


Assuming, my code doesn’t contain any errors that are affecting the results in a significant way, the following conclusions can be drawn:

  • Enabling WAL for a database gives a significant performance boost for all read and write operations.
  • If memory is not an issue, shared caches shouldn’t be used as they may decrease read performance.
  • Using read-only connections doesn’t affect the read performance.

Regarding shared connection vs. multiple connections:

  • If you only have one thread, it doesn’t matter (obviously).
  • If you do primarily reading…

    • … and the thread count is <= the CPU (core) count: use multiple connections
    • … and you have more threads than CPUs (cores): use shared connection
  • If you do primarily writing, use a shared connection.
  • If you do about the same amount of reading and writing, use multiple connections.

I hope this helps. If there’s something (terribly) wrong with this analysis, please leave a comment below.

Please note that these results are based on a Windows system. Other operating system may produce other results.