Showing posts with label c++. Show all posts
Showing posts with label c++. Show all posts

Sunday, February 17, 2008

Threading libraries in C++

Here is the list of Threading libraries in C++

Pthread (POSIX threads)
http://en.wikipedia.org/wiki/POSIX_Threads
pthread won't work fine on windows, because windows won't support POSIX standards - but there are some implementations of pthreads api on windows also!
Note: pthread is a c library not a c++ library, hence you can use it in c applications also.

Boost::thread - supports many compilers and architectures include Linux
and Windows.

Check these links: http://www.ddj.com/cpp/184401518,
http://www.boost.org/doc/html/thread.html

The problem with Boost libraries is that it requires good knowledge on STL, and learning is not very easy.

Glib::thread - Glib supports multi-threading, it can run on Windows and Linux but I don't think it supports VC++ compiler.

Check these:
http://www.gtkmm.org/docs/glibmm-2.4/docs/reference/html/classGlib_1_1Th
read.html, http://inti.sourceforge.net/tutorial/libinti/threads.html

Qt: Qt library (not the Quick Time player from Apple) also have threading support (both 3.x and 4.x versions). Qt 4.x is released under GPL for Windows also.

http://doc.trolltech.com/3.3/threads.html

Note: Nokia recently acquired TrollTech (the company behind Qt)

Intel threading library:
One more library from Intel (open source library, GPL2 with the runtime
exception). But only works on x86 or x86_64 (Intel or AMD; 32 or 64 bit)
architectures.
http://www.intel.com/software/products/tbb/
http://threadingbuildingblocks.org

wxWidgets also have a threading library.

And it is easy to write own threading library too!

Win32 is also having a thread api - but who is using win32 or MFC these days?

However I bet on either PThreads or Boost::Threads.

Anyhow in next version <http://en.wikipedia.org/wiki/C%2B%2B0x> of C++, standard library will support threading - API very similar to boost::thread library!

Working with Macros in C

More about C Macros

I do agree that everybody who worked in C, knows about pre-processor and macros.

But I thought of sharing some tips about preprocessor and the gcc options to control the preprocessor output.

Here is a simple usage of macros

#define sum(a, b) a+b

int main () {
int k = sum(10,20)*10;
return k;
}

Yes, the output is 210 (not 300, the famous interview question for freshers).

Let us check the output of preprocessor and see that the result is 210...

gcc takes an argument -E that output the preprocessor output.

Compile the above program using gcc -E test.c -o test.txt
Open the test.txt file and the output is

int main () {
int k = 10 +20*10;
return k;
}

Now you see how the #define is replaced with the value... quite useful for complex preprocessor commands.

Thought of add couple of other useful preprocessor commands.

For fun let us write a application that merges two XML files (by adding a root tag)... a cool application (not really, you can do it easily using a simple shell script ;-( ).

Combine two or more xml files using gcc?
Very simple.

sample_app.x

#include FILE1
#include FILE2

compile it using:
gcc -x c -P -DFILE1=\"text1.xml\" -DFILE2=\"text2.xml\" -DROOT_TAG=root1 -E sample_app.x -o result.xml
make sure that the text1.xml and text2.xml files are available. The output will be in result.xml (defined in -o option).
we use the following compile options:
-x c here our input file is sample_app.x, and gcc doesn't know whether it is C or C++ or Java, so mention that the language is c using -x option
-P (don't generate # line directives)
-D define the macro
-E only preprocess, don't compile and link
-o output file

Even though the above example is very simple, there are couple of very wonderful technologies that are build using this like
1) SQC uses this kind of logic (you too can write your own SQC compiler with some trouble).
2) Variable-length parameter list (called varags) also uses preprocessor (if you are not sure about this, just check that printf (a c function), can take any number of arguments)... you can also create variable length argument functions.
3) Template engines. just check the google CTemplate or Java Velocity (even though they are not written using this).

Some more tools while using preprocessor.
Predefined Macros
__LINE__ current line number
__FILE__
__DATE__ current date
__TIME__
__cplusplus defined if using a c++ compiler

Other useful macros defined by gcc
__SIZEOF_INT__
__SIZEOF_LONG__
__SIZEOF_LONG_LONG__
__SIZEOF_SHORT__
__SIZEOF_POINTER__
__TIMESTAMP__
__INCLUDE_LEVEL__

Stringizing using #
Common usage of this is escaping double quotes.
For example use what to copy a string
Error: "File Not found" - please check " test.xml"
We write:
printf("Error: %s\n", "\"File Not found\" - please check \"test.xml\"");
see the code looks ugly because of escaping double quotes, use Preprocessor to automatically include escape characters.

# define Q (x) #x
printf("Error: %s\n", Q("File Not found" - please check "test.xml")); // see we are not escaping any double quotes
Check the result using the same gcc -E option.

Token pasting using ##
A good example in gcc document related to this.
Say out want to generate a commands list like:
struct command commands[] =
{
{ "quit", quit_command },
{ "help", help_command },
{ "run", run_command }
...
};

where struct command
{
char *name;
void (*function) (void); // hope you are aware of function pointer...

// I can't give you more details because I too don't much about it :-(
};

you can define as:
#define COMMAND(NAME) { #NAME, NAME ## _command }

struct command commands[] =
{
COMMAND (quit),
COMMAND (help),
COMMAND (run);
...
};

Just run the above example using gcc -P -E test2.c -o test2.text and see the output!

Saturday, April 21, 2007

Benchmarks of different computer programming languages

Here is a bench mark of different programming languages,
http://shootout.alioth.debian.org/gp4/
http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=java

For example, a comparison between c (using gcc) and Java (Sun JDK, but just note that some time IBM JDK runs faster than Sun JDK)
http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=gcc&lang2=java
C application starts 91 times much faster than Java and take 22 times less memory in recursion.
Of course every one agrees that C is much faster than Java. But what is more important is the analysis of the results.
For example between C and Java, except that Java takes more memory and Start up is very slow, C is just 2 times better than Java.
Hence where memory is more and we won't start the application too often Java is better taking in to account its garbage collection and vast library. So, application servers (J2EE servers) are perfect for Java rather than Desktop application like notepad. Generally you re-start a server once in a month or so (hence start up speed of C 91 times better doesn't really matter here) and servers will have 5 GB RAM and hence whether the application takes 100 MB or 500 MB memory really is not a matter. Cool.

Now lets compare Java and Python
http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang= java&lang2=python
Even though Python is very slow compared to Java (in some case 90 times slower in recursion), it is much better in Memory (almost in every case, it takes less memory some time 10 times) and it starts 7x better than Java. Now you got the idea, Python is better for desktop applications as it takes less memory. Desktop application memory is more important. Image Java application takes around 20 MB, then you can even run 15 Java applications on a 512 MB RAM Desktop (taking into account OS will also take some memory), but Python takes 10 times less memory so you can easily run 150 Python application simultaneously. So, we can safely conclude that Python is very hot for Desktop application (of course C is better than Python, but comparing with the huge standard library Python provides, even bigger and cleaner than Java ;- Memory usage of Python is comparable with C), it is better for Desktop applications than C or Java where memory is more important than fast (yes, imagine a Notepad written in Java than takes 20 MB of memory, 2 minutes to start and then runs as fast as C notepad. Whereas python may take around 1 or 2 MB, takes around 10 secs to load but runs slower than C Notepad. But do the user really able to differentiate a program that take 2 ms or 200 ms, here even though the other program is 100 times faster than C, it doesn't matter in this case, it matters in RDBMS or Search engines Right?).

http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang= python &lang2=perl
Comparing Perl and Python, see that except in one case) both are equally good. So now we should consider only the Language, library, and portability advantages. For some fun we will compare these now.
Python is a very clear Object oriented language, on the other hand Perl supports both OO and Structural programming. Python has a big standard library set than Perl. But developing small proto-type application in Perl is far easier than Python (in Python you need to define a class, methods). So, perl better as a scripting language or cgi programming rather than for developing large scale applications (because for large applications Perl looks cluttered and Python look very organized).

So, which language is better? It depends on the type of application we are developing.

Regards
Suresh

Copyright (c) 2008 - Suresh