A little benchmark with C++ and Java in the game.

Today i just wanted to make a benchmark and see which is faster java or C++. There are rumors over the internet that java is or will be faster than c++ some day. So today I decided to try compare the speed of those languages. We all know arrays and how bad they can be sometimes if you are not careful with them in C++, because

    • They don’t know it’s size


  • You cannot extend it if you reach the limit



  • The name of an array converts to a pointer to its first element at the slightest provocation(there are nice examples for this point here)



So with one word arrays are not nice. Java provides some fixes for those, because there an array know it’s size and there are no pointers :) . But what if we use a higher level abstractions from STL? Will std::vector beat the Java arrays? Here is the code:

#include <vector>

#include <iostream>

int main(int argc, char* argv[]){

std::vector<int> Vec(10000);

for(int x = 0;x < Vec.size();x++){

Vec[x] =x;

std::cout << Vec[x] << std::endl;




class Main{

public static void main(String []args){

int Vec[] = new int[10000];

for (int x = 0;x < Vec.length;x++){

Vec[x] = x;





So we compile with

g++ -O2 -fomit-frame-pointer main.cpp && javac Main.java

and let’s run them through time :)

nikolavp@Nikolavp:~$ time ./a.out > /dev/null

real    0m0.010suser    0m0.004ssys     0m0.004snikolavp@Nikolavp:~$ time java Main > /dev/null

real    0m0.196suser    0m0.140ssys     0m0.052s

So C++ with vector which is supposed to be a higher level abbstraction outperformed Java’s “safe” arrays. The next time you want to write something in Java think a little bit is it really worth the machines ;) . Don’t get me wrong Java have it’s place and it made the whole world a better place, but don’t convince me, that it will be faster(or is faster now) than C++… It just never will.

using namespace std;
int main(){

for(var i=0;i{
click("sign in")

Nice way to keep the lines indented in vim

Here is one more vim tipp. I just put a new keybinding to my .vimrc file, thanks to \amethyst from #vim@freenode.net :) . Here it is

inoremap <expr> <CR> (col("$")==col(".") ? "\<ESC>=a{\<C-O>o" : "\<CR>")

This will indent the whole block before the line if you hit <CR> on the end of the code you are writing in insert mode. If you are not at the end of the line you will get the default behaviour of <CR>. I was not sure how to implement that, but \amethyst really helped me on IRC. Once more this shows the power of chatting with experienced people who know the project very well. Viva open source :) .

Highlight for Qt classes in vim

Maybe some of you like me like to use a sophisticated library for C++ development. My choice is Qt. I really like to have highlighting for most of the classes so here is my ~/.vim/syntax/cpp.vim. This will highlight all of the Qt classes as built in structures of the language, which is really nice. I just copied all of the classes from the Qt’s web site ;) .

Today Vidas Katinas send me the modified version so i have updated the link. I have also added a checking for a variable. I am also using stl lately so here is the installation guide if you use them both:

mkdir -p ~/.vim/after/syntax/cpp
wget -c http://nikolavp.googlepages.com/cpp.vim -O ~/.vim/after/syntax/cpp/qt.vim
wget -c http://www.vim.org/scripts/download_script.php?src_id=8628 -O ~/.vim/after/syntax/cpp/stl.vim

Now if you want to disable the Qt highlighting just put that somewhere in your vimrc file

let g:qt_syntax=0

If for example you want to disable the syntax for the current file(it is too slow ot whatever) type this

:let g:qt_syntax=0:e



if you have made changes.

Making abbreviations persistent in vim

If you are like me, most of the time you make more mistakes in vim, because your mind is little slower than you fingers. Enter abbreviations which is a really neat feature in vim :) . You just have to type

:iabbrev wrongstring wantedstring

in command mode and the next time you type the word wrong vim will fix it. So where is the problem? The problem is that when you type that in command mode it is not persistent between sessions. I really wanted to save those save somehow in a file. So here is the solution :)

function! Abbreviate(input, output) python << EOFdef appendAbb():    import os    import vim    home = os.getenv("USERPROFILE") or os.getenv("HOME")    home += "/myvimfiles/abbs.vim"    Abbs = open(home, "a")    inputstr = vim.eval("a:input")    outputstr = vim.eval("a:output")    abbreviation = "iabbrev " + str(inputstr) + " " + str(outputstr)+ "\n"    Abbs.write(abbreviation)EOF:py appendAbb()let abbrevation = "iabbrev ". expand(a:input). " ". expand(a:output)execute abbrevationendfunctioncommand! -nargs=+ Abb :call Abbreviate(<f-args>)

Put that in your ~/.vimrc file and you are done :) . You should change /myvimfiles/abbs.vim to the file with abbreviations you would like to use. If it is in a special directory not in your runtimepath you should also put source “path/to/the/file” in vimrc :) . Now next time you want to correct a type just type

:Abb wrongstring correctstring

and that’s all ;)
Happy vimming

Renaming a variable in gvim

Some of you may be missing the nice IDE’s and their neat features when you are editing in gvim. I was myself a long time Eclipse user, but I found that it doesn’t make me more productive, but just eat the resources of my machine. I missed the renaming variable feature in gvim for some time now. Today this is over I was reading the manual of vim and found what I was looking for. We all know the substitute command, but I was not sure how to replace the variable with one command for all files in the project. So here is what I have now in my .vimrc:

fun! BuffersRenameVar(cword, newvar)        let word = a:cword        let newvar = a:newvar        let replace = ":bufdo %s/\\\

You just hit \brn for buffer rename and have to change the “nvname”
with the new name of the variable and that’s all :) . This will ask you
to rename every appearance of the variable in all open buffers. Don’t
forget to open all all your files in vim. This will also put a global
mark named “R” so you can continue your work. Happy vimming