After I started benchmarking how much time commands took to complete in
Dungeon, I noticed that writing a full world map (a grid of colored ASCII
characters) to the screen was reasonably slow. Then I set forth to find the
solution to my problem.
Basically, there are two causes for slow
JTextPane updates: each insertion
triggers a re-rendering of the display and some methods in the
DefaultStyledDocument class are synchronized, so there is the overhead of
I worked on the first issue and was able to reduce the overhead of insertion
substantially. I wrote a class that keeps two
StyledDocuments: one inactive
and one set to the
JTextPane the class is responsible for. When text is
written to this object, it will update the
StyledDocument that is inactive,
swap both documents, and then update the now inactive
keep two documents? Some insertions are just small increments, that really
benefit from not having to rewrite a long passage of text multiple times.
How to make it faster? Instead of running the second document update (the user
has already seen the update one because the swap has already taken place) on
the same thread, delegate it to a worker thread. Note that this approach will
block this object for a while. To keep it simple and avoid premature
optimization, I did not even code this multithreaded version. But I am
mentioning it here just in case someone (maybe me) needs it in the future.
For reference, the issue related to the
Recently, I found myself wondering how much memory a Python list I had in my
environment was using. So I searched and found the following very handy
function for the curious minds or desperate debuggers.
Return the size of an object in bytes. The object can be any type of
object. All built-in objects will return correct results, but this does
not have to hold true for third-party extensions as it is implementation
Only the memory consumption directly attributed to the object is
accounted for, not the memory consumption of objects it refers to.
If given, default will be returned if the object does not provide means
to retrieve the size. Otherwise a TypeError will be raised.
getsizeof() calls the object's __sizeof__ method and adds an additional
garbage collector overhead if the object is managed by the garbage
I was trying to install MongoDB 3.0 in my production machine and in order to be
able to do that via
apt-get I needed to import the MongoDB GPG public key.
It went wrong. It went really wrong.
After a huge delay at
gpg: requesting key 7F0CEB10 from hkp server keyserver.ubuntu.com
I would get
gpg: keyserver timed out
gpg: keyserver receive failed: keyserver error
The first solutions I found on the internet for this problem were modifying
other commands to look like mine. So I already had the “best command”.
After some more Googling I found that I could grab the key manually and add it
without a request to a server.
So I tried and got the key.
sudo apt-key add 10gen-gpg-key.asc
And the shell told me
That was it. I decided to post about this so that if you find yourself in a
similar scenario (cannot get keys via the command line), you have an idea of
how to solve the problem. It also serves as a future reference to me, as
StackOverflow didn’t do it this time.
This post is another small git tip. If you ever need to push to a remote branch
with a different name than your local branch, use
git push origin local-name:origin-name
So if you want to push your local
dev into your origin
review you’d do
git push origin dev:review
In a train yard, train cars are rolled down a long hill in order to link them
up with other cars as shown. A car of mass 4000. kg starts to roll from rest at
the top of a hill 5.0 m high, and inclined at an angle of 5.0° to the
horizontal. The coefficient of rolling friction between the train and the track
is 0.050. What velocity would the car have if it linked up with 3 identical
cars sitting on flat ground at the bottom of the track?
Hint: The equation for rolling friction is just like the one for sliding
The speed of the car after leaving the hill is equal to
v = sqrt(kinetic_energy * 2 / m)
And its kinetic energy will be equal to the work done on it:
Getting the distance is a matter of simple geometry:
d = csc (5°) * 5 m ~= 57.37 m
The force is a bit more complicated, note that I am using the net force, taking
friction into account
F = tan (5°) * 4 * 10^4 N - 0.05 * sec (5°) * 4 * 10^4 N
= 4 * 10^4 N * (tan (5°) - 0.05 * sec (5°)) ~= 1,492 N
Throwing everything together
v = sqrt(F * d * 2 / m) ~= 6.542 m / s
As the velocity will be equally divided between the cars
v = (6.542 m / s) / 4 = 1.635 m / s
Thus, the answer is 1.64 m / s.
Note that I used
10 m s^-2 for the gravitational acceleration. If you want to
be more precise, use
9.80665 m s^-2, the the standard acceleration due to