Something that has been on my mind of late

DRAFT POST

I very much need to start standardizing myself on a smaller set of tools. Maybe tools is not the best choice of ord, o much as it is a question of languages and libraries. It’s actually kind of ironic, I really would like to learn several more languages (Ada, Fortran, C#, Erlang, and I never did wrap my head around Scheme).

C++

Pro’s:

can do low level stuff

can interface well with C code and libraries

Qt is natively C++ and supports everything I need it to run on (in Qt4 OSE hehe)

Fairly portable and standardized (at least much of the 1998 standard…) with several compilers available.

Mainly toolkits and frame works available, both native and from C.

Widely used and my first language

Con’s:

having to dig into lower level interfaces is very error prone when doing it with a headache the size of California.

Often not my first choice for building a prototype *quickly* but good for final implementation.

Java

Pro’s:

Inheritable portable between JVMs of the same implementation (e.g. Sun JVM for Win and Sun VM for Mac can run same code).

Simpler OOP syntax then C++, imho

The Swing GUI toolkit is fairly portable and SWT sufficiently supports the platforms I want to avoid skipping.

I like the way it handles exceptions, and usually like checked exceptions — when a class is designed appropriately.

It’s well defined if not perfectly standardized and compliant implementations are fairly common enough (Sun’s)

Wildly used and my third language.

Well documented (if a bit boring)

Con’s:

Everything is OOP….

I prefer native code to waiting on java to load

Requires a suitable runtime

Conventions expected by some tools can occasionally be irksome (to me)

C# (C Sharp)

Pro’s:

It’s not Java

It’s similar to Java/C++

Core elements are standardized

Less resistance to shooting oneself in the foot / doing stupid things then Java but not as much as C++.

Gtk#, the binding of GTK+ to C# is available on the platforms I care about.

Con’s:

Most C# applications are probably tied to Microsoft Windows implementation via .NET or through Gnome related interfaces

Requires a runtime (e.g. .NET, Mono, or Dot GNU)

Requires just enough learning of it’s differences from Java, for me to use it.

PHP5

Pro’s:

Portable and interpreted

Widely used across the WWW for server side scripting.

GTK+ bindings to PHP available and portable’ish

Simple language and member of the CBF.

The OOP increasingly resembles Java syntax and is just “enough” syntax to be useful without cramming.

Well documented

Con’s:

Local php.ini files can cause problems (how much can you assume?)

Needs a run time (php) of the right version (5)

php4/php5 is less common off web servers or developers systems

Python

Pro’s:

Great for writing prototypes

Several toolkits available that are fairly portable

Easy to work with and quit portable (and issues of portability well noted in the docs)

Large standard library

Implementations for the Java Virtual Machine and Microsoft .NET framework are available (if not as current as CPython) as well as the standard (C)Python implementation.

Con’s:

Requires a run time

I *hate* it’s handling of regular expressions after being so used to Perl…

It’s slower then native code

Interfacing with C /or C++ code can, uhh… Get interesting, from a portability perspective.

The program is the source

Ruby

Pro’s:

Handy pure OOP language

Great handling of regular expressions within the language itself, as opposed through importing an object oriented interface (i.e. as in Python/Java/C++)

Usually very “comfortable” to write.

Large standard library

Con’s:

It requires a run time and alternative compilers (e.g. xruby, ironpython) may be lacking in reliability or cause some features to be unavailable

The program is the source

Wishing Ruby 2 would come sooner….

Documentation can be irksome at times

It’s slower then native code and even slower then Python (Ruby 1.8.x)

JavaSwing.strange()

This is very funky. I downloaded a tutorial file from sun for Java’s Swing toolkit and it compiles fine but can’t find the main thread when compiled & run as directed.

I comment out the ‘package start;’ at the top, recompile and it runs fine >_>

To be honest, I think I’m sort of happy I’ve never written a lot of Java code… Especially because most encounters whereByPeopleWhoHaveTypeNames andVarNamesThisLong which kind of pisses me off lol.

I do have Qt Jambi installed on my desktop but the problem is family really makes it easier to write code off my laptop, since I get get further away from the line of fire… Which means without a fair bit of tinkering, it’ll be easier to write the GUI in Javas Swing.

I did learn a little of AWT way back when but I doubt any of it is still good by now lol.

Brushing up with Java

It’s been quite an interest of mine to brush up with Java and Perl for awhile now but time has never really allowed me to do so.

Java for me is a language to which a lot of knowledge is stored away in the ol’brain but little wisdom so to speak. Java was maybe my 3rd language but I’ve also done a pretty good job of writing almost no Java code over the years hehe. I’m used to the syntax though. One day the Library had a sale of old books and one was the Java Sourcebook by Ed Anuff.

This was maybe 2006 and the book was so old that some where in the interface I think it said that JDK 1.0 Beta would be released by the time people were buying the book. Until more recent aspects involving Python and C++ with Qt3 / Qt4 its introduction (unwritten) to Javas Abstract Window Toolkit (AWT) was the closest I ever got to GUI work.

It is a very good book IMHO but of course very dated, I don’t know how much java.lang.applet and whatnot has changed over the years, I never really cared much for Java applets! But I am pretty sure that a lot is different with AWT and the newer Swing toolkit. One thing that I actually find interesting, the Java Sourcebook is maybe the only book I’ve seen that uses the same brace style I tend to use in C. Hmm, maybe it was an influence on my ways of doing things :

Not to long ago I checked out a nice O’Reilly book on Java because I wanted to see how much the language had changed in the past ten years or so. Java indeed changed quite a lot, it is way more fatter then I remember but it is still just as sweet.

One thing I like about C, the syntax is small enough to cram in ones head easy and follow it up with a reference of the standard library for stuff you don’t use often. Java OTOH is probably a few orders of magnitude larger then that for most people. With the things I noted, inner classes, generics, etc I was a little suprised that they were there but the language seems to have grown naturally. As I read though parts of the book I was constantly like “Oh, that works just like I expected” when comparing the detailed explanations of how things work along side the code listings.

C++ was my first language aye but I think I’m more comfortable with Java. Because in the fields where C++ differs from C, I tend to get a little more lost, while with Java most of it is already there.

Perl, my second language but probably the weakest in my skill set. I quickly got board and skipped a lot which gradually filtered through over the years. I respect Perl for its power and flexibility but I think it has gotten a little to big for its own good IMHO, although I wait to see what Perl 6 will bring us.

I remember in the Java Sourcebook there were several examples, such as an implementation for Lamp and LightBulb classes that interacted and various cat related ones that used a speak() method to examine how overloading methods and inheritance worked.

This is a reworking of it from memory, same basic idea though:

Jeez now that I think of it, those examples remind me of stuff from an UnrealScript tutorial I looked at a couple years back too. Who knows with how much UnrealScript feels like a customized Java they probably read the same book hehe.


/**
* Feline.java, abstract representation of felines
*/
abstract class Feline {
abstract void speak();
}


/**
* HouseCat.java, implements a feline that meows
*/
class HouseCat extends Feline {
HouseCat() {
}
HouseCat(String call) {
this.speak(call);
}
void
speak() {
System.out.println("Meeeooowww!!!");
}
private void
speak(String call) {
System.out.println(call);
}
public static void
main(String[] args) {
HouseCat theCat,newCat;
theCat = new HouseCat(); /* use default constructor */
newCat = new HouseCat("I am a cat!");

theCat.speak();
}
}

The HouseCat class implements a Feline (abstract meaning you can subclass it but you can’t create it directly). There is a simple constructor made to behave like a default constructor and one that invokes a private version of the speak() method using a supplied string.

I have the Java Development Kit and Run Time Environment installed from the packages on the FreeBSD Foundation website. Compiles fine with javac and gcj42, that is the GNU Compiler for Java ;-).

What I do like about Java, its fairly portable between systems running the same implementation (e.g. Suns JRE) as far as language features go. It also follows a largely logical behavior for many things from what I have seen.

And to top it all off, GCJ can compile to native machine code rather then Java Bytecode — for me a huge plus. I do have Qt4 bindings for Java installed on my desktop so I am interested to see where that goes, as far as my laptop is concerned I would probably need to use Suns compiler for AWT/Swing apps though. Not sure if Qt Jambi is in FreeBSD ports yet either.

As much as I love C, there are just some things that I don’t really want to have to implement in it. And as productive as I am in Python and Ruby I really need to cuddle up with some thing a bit closer to C’s end of the spectrum. I don’t really care a lot for C++, I can use it fine but don’t see much point for it over C besides templates and exceptions. Java lacks pointers in the same light as C/C++ uses but the references should be fine enough for my general needs.

C++ is multiple inheritance and Java is single, I’m not really partial to either concept as long as nether takes to biting me often. I have long enjoyed the design of Java’s classes and interfaces though, I love the interface thing in Java.

I don’t exactly care much for Suns compiler though but hey, if it works (y).

Wasting time with the Euclidean Algorithm

The other night, I was very bored so… When I remembered reading about the Euclidean Algorithm on Wikipedia, which is a method of finding the greatest common denominator (gcd). I fed several implementations through ye ol’time(1) to get a rough idea of what differences they made.

At first I did it in Ruby and C for comparison, then I recompiled the *.c files with maximum optimization. Tonight I added a set of Java and Python files to the setup, I’ll probably include Bourne Shell and Perl later for fun.

For any one interested,

C // no optimization
./iteration 0.00s user 0.00s system 50% cpu 0.003 total
./recursion 0.00s user 0.00s system 66% cpu 0.002 total
./original 0.00s user 0.00s system 57% cpu 0.003 total
Ruby
./iteration.rb 0.01s user 0.00s system 59% cpu 0.014 total
./recursion.rb 0.00s user 0.00s system 79% cpu 0.010 total
./original.rb 0.00s user 0.01s system 75% cpu 0.010 total
C // optimized, -O3
./iteration-o 0.00s user 0.00s system 48% cpu 0.003 total
./recursion-o 0.00s user 0.00s system 32% cpu 0.005 total
./original-o 0.00s user 0.00s system 37% cpu 0.004 total
Java
java EuclideanIteration 0.39s user 0.38s system 66% cpu 1.165 total
java EuclideanRecursion 0.48s user 0.30s system 72% cpu 1.066 total
java EuclideanOriginal 0.36s user 0.42s system 67% cpu 1.155 total
Python
./iteration.py 0.01s user 0.01s system 59% cpu 0.034 total
./recursion.py 0.01s user 0.01s system 65% cpu 0.032 total
./original.py 0.01s user 0.01s system 65% cpu 0.031 total

done with:

ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-freebsd6]
gcc version 3.4.6 [FreeBSD] 20060305
javac 1.5.0
Python 2.5.1

The C versions were the same sources but compiled with -O3 for the optimized
version.

I’ve assigned each outcome a score, 3 for what I feel is fastest, 2 for the intermediate (often close) and 1 for the worst and totalled it:

method  C RB C(-O3) Java Python Total
iteration 2 1 3 2 2 10
recursion 3 2 2 1 1 9
original 1 3 1 3 3 11

And the code, which I tried to keep similar. Also the gcd()/mygcd() routines were always implemented as a function because of the recursive version in the tests.

#include <stdio.h>

#define A 1071
#define B 1029

int
mygcd( int a, int b ) {
int t = 0;
while ( b != 0 ) {
t = b;
b = a % b;
a = t;
}
return a;
}

int
main(void) {
mygcd(A, B);
return 0;
}


#include <stdio.h>

#define A 1071
#define B 1029

int
mygcd( int a, int b ) {
if ( b == 0 ) {
return a;
} else {
return mygcd( b, a%b );
}
}

int
main(void) {
mygcd(A, B);
return 0;
}


#include <stdio.h>
#define A 1071
#define B 1029


int
mygcd( int a, int b ) {
while ( b != 0 ) {
if ( a > b ) {
a = a-b;
} else {
b = b-a;
}
}
return a;
}

int
main(void) {
mygcd(A, B);
return 0;
}

#!/usr/local/bin/ruby -w

def gcd(a, b)
while b != 0
t = b
b = a % b
a = t
end
return a

gcd( 1071, 1029 )
#!/usr/local/bin/ruby -w

def gcd(a,b)
if b == 0
return a
else
return gcd(b, a % b )
end
end

gcd( 1071, 1029 )
#!/usr/local/bin/ruby -w

def gcd( a, b )
while b != 0
if a > b
a = a - b
else
b = b - a
end
end
return a
end

gcd( 1071, 1029 )

class EuclideanIteration {
static final int A = 1071;
static final int B = 1029;

public static int
mygcd( int a, int b ) {
int t = 0;
while ( b != 0 ) {
t = b;
b = a % b;
a = t;
}
return a;
}

public static void
main( String[] args ) {
mygcd(A, B);
}
}



class EuclideanRecursion {
static final int A = 1071;
static final int B = 1029;

public static int
mygcd( int a, int b ) {
if ( b == 0 ) {
return a;
} else {
return mygcd( b, a%b );
}
}


public static void
main( String[] args ) {
mygcd(A, B);
}
}


class EuclideanOriginal {
static final int A = 1071;
static final int B = 1029;

public static int
mygcd( int a, int b ) {
while ( b != 0 ) {
if ( a > b ) {
a = a-b;
} else {
b = b-a;
}
}
return a;
}


public static void
main( String[] args ) {
mygcd(A, B);
}
}

#!/usr/local/bin/python

def mygcd(a, b):
while b != 0:
t = b
b = a % b
a = t
return a


mygcd( 1071, 1029 )
#!/usr/local/bin/python

def mygcd( a, b ):
if b == 0:
return a
else:
return mygcd( b, a %b )


mygcd( 1071, 1029 )
#!/usr/local/bin/python

def mygcd( a, b ):
while b != 0:
if a > b:
a = a-b
else:
b = b-a
return a


mygcd( 1071, 1029 )

FreeBSD + Java?

How easy is it…

The JDK and JRE packages on the FreeBSD Foundation require the following to install and run correctly:

inputproto-1.4.2.1, javavmwrapper-2.3, kbproto-1.0.3, libICE-1.0.4,1, libSM-1.0.3,1, libX11-1.1.3,1, libXau-1.0.3_2, libXdmcp-1.0.2, libXext-1.0.3,1, libXi-1.1.3,1, libXp-1.0.0,1, libXt-1.0.5, libXtst-1.0.3, pkg-config-0.22, printproto-1.0.3, recordproto-1.13.2, xextproto-7.0.2, xproto-7.0.10_1, xtrans-1.0.4

If you run a Desktop based system with Xorg installed most of these are probably already there, on my laptop which has KDE 3.5.7 for the GUI all I had to do was install one package first:

pkg_add -r javavmwrapper

If you don’t have a required package it will tell you what you need to install when you try to pkg_add Java. Most probably can be pkg_add -r’d unless you want to the ports.

I then went over to the FreeBSD Foundation website to download Java

If you only want to run Java programs, you will need the JRE (Java Runetime Environment) but if you wish to develop Java programs you’ll require the JDK (Java Development Kit).

The packages currently are for FreeBSD 5.5/i386 and FreeBSD 6.1/i386 and amd64. Just download the one that matches your FreeBSD major branch and CPU Architecture. I fetched both JRE and JDK for FreeBSD 6.1/i386 which is about a 70MB download combined, my Laptop runs PC-BSD v1.4 so the underlaying system is FreeBSD 6.2-Stable, no problems so far.

You need to accept the license agreement to download and when you pkg_add, just scroll to the bottom and type ‘yes’ and press enter, you can probably use the shift+g command to jump to the bottom of the license.

I think because of the terms that the JRE/JDK were licensed for distro we’ve got to live with the manual download. Once you’ve got your packages downloaded, just do a :

pkg_add ./diablo-jdk-freebsd6.i386.1.5.0.07.01.tbz

of course replacing ./ with the path (if necessary) and the filename with the proper one. Tip: If you don’t want to typo it, use tab completion in your shell. I usually just download things to /tmp and cd over to it. You also need to be root in order to run pkg_add with any possibility of success. Depending on ones shell a ‘rehash’ command might also be needed before the programs can be found in the command prompt.

which tells me that java and javac are working, and it passed the test of compiling a small test program and running it.