Archive for the ‘Tech’ Category

Fun with timezones

Thursday, April 9th, 2009

Using dates and times in a program is not always as straight forward as you might think.  The issue is with timezones and daylight savings.  Most people forget to use a timezone when they store dates.  This might not seem like an issue if you think your program will never be used in another timezone, but it still is.

Timezones tell you how to convert your datetime from one timezone to the other.  10am in NYC is not the same 10am in Tokyo.  What makes this all the more fun is the wonder idea of daylight savings time.  Time will go backwards and forwards or seeming random days.  Suddenly there are 2 different 11:59pm.

When you look at a file in windows explort, did you ever think about the missing timezone on the dates?  You probably didn’t give it a second thought.  But you should.  You probably assume that it is showing the times in your current time zone.   Sadly you are more correct then you think.  I found out about this after I read this link.  It seems windows uses your CURRENT timezone to display the datetime.  Whats the problem with this?  If a file was created at 3/1 10:00pm and you look at the file in April, windows shows the file date as 11:00pm, even though the correct time should be 10:00pm.

Windows is smart enough to store the date in UTC/GMT/ZULU time, but when converting it back to your local timezone, it just uses your CURRENT timezone offset, instead of figuring out what it was on that date with DST factored in.  I wonder if someone was trying to optimize out a few CPU cycles.

So next time you are looking at the access times on your files, remember that they could be up to an hour off from what the actual time was.

Swap with out the tmp

Tuesday, January 27th, 2009

When swapping variables most people use

tmp = a
a = b
b = tmp

This requires using an extra variable tmp to store A while it is overwritten with b.  Wouldn’t it be great if you could swap the two without using an extra variable?  The XOR swap algorithm is just what you need

a^=b^=a^=b

This ends up with A and B swapped.  Still 3 assignments but with out the extra variable.  If you want a good explaination of how that works read the wikipedia article.

A word of warning though, the above code only works in C/C++.   It will not work in an VM language like Java or .NET.    This has to do with how the languages are implemented.  Java and .NET are stack based VMs where as C/C++ is register based (and not a VM).  This means that values are pushed onto the stack and then poped off to calculate values.

For example

int a = 1;
int b = 1;
int c1 = (a++) + a;
int c2 = b + (b++);

c1 will be set to 3 and c2 will be set to 2.   Written in C/C++ both will give the answer of 2.  The operator a++ will increment a by 1 but return its original value.  With a stack base, the 1st part of the equation, (a++), is calculated and the answer ( 1 ) push onto the stack.   The next part of the equation (a) is calculated and the answer (2) and push onto the static.   Why is the 2nd part of the equation 2?  When the add operation is run, it retrieves the actual values of the variable one of which is the returned ‘original value’ of a and the other is the current value of A.

An alternate way of writing the XOR swap that works on stack based (and non stack based) VMs is as follows.

a^=b
b^=a
a^=b

Note:  This does not work if A and B are the same object.  You shouldn’t be swapping in the case any ways.

The short moral of the story is dont use shortcut code to put a bunch of commands on the same line.  It may not do what you think it will.

Launch Wrapper

Sunday, September 2nd, 2007

Here’s a small utility i wrote after having to keep adding the same command lines arguments to a java. This program is a wrapper around the one you want to launch. You can specify a prefix and a postfix to the arguments. You can also alter them through a regular expression. This is all done through the .config file. The basic idea is to rename your executable, point the wrapper to the renamed file, and rename wrapper.exe to the original name of your executable.

wrapper.zip
Edit:  Updated exe to C++.    C#/.NET doesn’t allow exec like processes

Faster flash

Wednesday, May 2nd, 2007

I have been working on a project in Adobe flash for the last few months. It basically does a ton of animation and math on every screen. So i put all the code in onEnterFrame, the standard way to run continuous code, so it ran on each frame. Over the months I have done by best to optimize it for speed and efficiency but it had still ran slow, and used a lot of CPU power. How can I make it faster I thought?

The first most obvious thing to try was to up the FPS. Flash defaults it to 12, which seems rather slow for animation doesn’t it? So I updated it to 30, which didn’t seem the to change anything. 60fps offered a slight improvement. So it logically concludes that 120fps (the max you can set in flash) would give the best performance, and it seemed like it did. But the program was still using a ton of CPU power, and had a very low fps.

Was there a way i could squeeze a few more fps out of the program? Well there happens to be a function called updateAfterEvent() which basically tells the program to issue another screen refresh immediately. The only problem is that you can only call it from certain functions., one of which is NOT onEnterFrame. It would only work if called from an event, such as a mouse movement or key press. It also would work for a periodically ran function called through setInterval(). This looked like the option to try.

The function has this basic format:

setInterval(function, ms, params)

It takes a pointer to a function, the number of milliseconds in between calls to the function, and any params you would like to send it. That sounded wonderful, just set a really low ms, and pass in the function that does the same thing my current onEnterFrame does, and it should run as fast as I want. Well, not really. The one gotcha with this function is that it will only call the function at most, as often as onEnterFrame is called. The resolution of this timer is only as fine as the FPS in the movie. Even if i set the ms to 0, it would still only be called as often as onEnterFrame. Back the square one again, or was I? The new difference now was that I could call updateAfterEvent in the function, because it was called through setInterval. Yea! So i put the new code in, removed the old onEnterFrame, left out updateAfterEVents, set the ms to 10 and watched to see if there was any improvement.

Suddenly there was a 10x speed up. The animation wasn’t as sluggish and it responded to the uses key presses immediately. That didn’t make any sense. The same amount of code was being ran on every frame, the program should run the same speed. I tried changing the ms down to 1 and the animation only got faster. I changed the FPS of the program down to 12, and the animation was still just as smooth. I wondered if the function was being called more often the the screen updated, so I turned the FPS down to 1. A FPS of 1 resulted in jerky animation just as expected. So the function was still only being called as often as the screen updated. Turning the fps back to 12 (the default). I ran the program and watched the cpu usage, it was only around 10-20%, instead of the whopping 70% it was using before. Changing the FPS had the most effect on the CPU usage. A higher FPS resulted in more processor usage. But changing the MS in setInterval had barely any effect on CPU usage. Hmmm…. So i went around and changed all my onEnterFrame to setInterval code. This resulted in a much more responsive program, that had lower CPU usage. I still don’t know what the problem with putting code in onEnterFrame is, but I dont think I’ll be useing onEnterFrame much anymore.

The moral of the story:

DO NOT USE onEnterFrame in flash, its slow. Only use it if you absolutely must, but I don’t know why you would.
Changing the FPS does NOT result in faster code. Only MORE CPU usage.
USE setInterval(…) instead with a lowish FPS (such as 12)

Non-repeat random playlist in constant time?

Saturday, January 13th, 2007

I’m sure you have all heard about the problems with certain mp3 players and their ‘random’ play lists. Some people think they repeat some songs more than others. Well here a way to get a truly random shuffle of the list in constant time. That means it takes the same amount of time no matter how big the list is.

This class takes takes an Array of items (data) and divides it into two sections. The first section is the already randomized items, the other is the items that haven’t been shuffled yet. When you request the next item, it picks a random item from the remaining set and swaps it onto the end of the shuffled set. When the non-shuffled set is empty, it starts from the beginning of the array again. At this point the whole array would have been shuffled already so no more swapping will be needed.
Here is the entire class in C#
class Shuffle
{
Random r = new Random();
Object[] data;

int split; // where the split is in the array
bool shuffled = false; // has the entire array been shuffled
public Shuffle(Object[] q)
{
split = 0;
data = q;
}

public Object Next()
{
if (split == data.Length)
{
split = 0;
shuffled = true;
}

if (!shuffled)
{
// find the next one
int idx = r.Next(data.Length – split);
// swap the positions
Object tmp = data[split];
data[split] = data[idx];
data[idx] = tmp;
}

return data[split++];
}
}This class shuffles the list in constant time. It only shuffles when a new item is requested from the list, and only shuffles that one item. This is many many times faster than shuffling all the items at once.

Enjoy.