.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:

ArrayClearTest.cs

Upload arbitrary files to WordPress (new plugin)

WordPress is very restrictive when it comes to file uploads. It’s for security reasons, mainly to prevent bad-behaving users to upload PHP scripts and the like to the blog.

However, if you’re the only one writing posts for your blog, this restriction sometimes is annoying. For example, I do a lot coding in C#. So, when I try to upload a .cs file (C# source code file) to my blog, the upload is rejected.

Wordpress rejected upload with "Sorry, this file type is not permitted for security reasons."

Files are approved or rejected based on their file extension, i.e. the few characters after the dot in the file name, like cs in ArrayClearTest.cs.

WordPress maintains an internal list of which file extensions are allowed. Fortunately, WordPress also allows for this list to be extended.

And that’s what my new WordPress plugin does. It’s called Upload File Type Settings Plugin and allows you to extend that list with an easy-to-use user interface. Go, give it a try.

The plugin's settings page.

Bug of the Day: VMWare Fusion has encountered an error…

This new daily (*cough*) post category will document me being frustrated about the software I use. I have the feeling that I encounter a new bug everyday in some software that I use. Let’s see whether this holds true.

First entry:

VMWare Fusion has encountered an error and has shut down the virtual machine.

vmware-fusion-error.png

Solved it by disabling “Accelerate 3D graphics” for the machine.