StokeBloke.com

Archive for the ‘Programming’ Category

emacs line wrapping

Thursday, April 24th, 2008

By default emacs has line wrap enabled for the whole window. I.e if you only have 1 buffer open then it will line wrap at the edge of the window.

If you open many buffers though there is no line wrapping on each buffer.

If you add the following to your .emacs file it will make side by side buffers behave like the full window.

(setq truncate-partial-width-windows nil)

I dont like line wrap, especially when coding so I set the following

(setq default-truncate-lines t)

I then use f12 to toggle the line wrap by adding the following

(global-set-key [f12] 'toggle-truncate-lines)

So my whole line-wrap.el file is

;; See http://www.delorie.com/gnu/docs/elisp-manual-21/elisp_620.html
;; and http://www.gnu.org/software/emacs/manual/elisp.pdf

;; disable line wrap
(setq default-truncate-lines t)

;; make side by side buffers function the same as the main window
(setq truncate-partial-width-windows nil)

;; Add F12 to toggle line wrap
(global-set-key [f12] 'toggle-truncate-lines)

See 38.3 Truncation or the emacs manual for more information.

emacs refresh F5 key

Thursday, April 17th, 2008

I use this little addition to my .emacs file to support reloading the current file I’m editing.

(defun refresh-file ()
  (interactive)
  (revert-buffer t t t)
  )

(global-set-key [f5] 'refresh-file)

You can also use CTRL-x CTRL-v to do a “Find alternative file” and choose the same file that you are currently editing. F5 is a little quicker though.

dont use stdio getc() or fgetc(), they are slow

Tuesday, April 15th, 2008

Well I thought I would write about an issue I have known about for some time, but it appears others still dont.

Its regarding stdio fgetc() performance. Mainly that using fgetc() is always slow and should rarely be used.

Here is a comparison of fgetc(), fread() and fgets() when reading a 333Mb mbox file created via thunderbird.

operation time
fgetc() 7.15 seconds
fgets() 0.65 second
fread() 0.08 seconds

Times are take on a quad core 2.4Ghz (linux 2.6.2.4 tuxonice r4 gentoo)

The performance issues clear show that retrieving information in larger ‘chunks’ is more efficient and its scary how different the performance is. Unless you only want 1 or a few characters, you should probably not use getc()

The code

fgetc()

clock_t start;
clock_t end;
FILE* testFile;
char buf[8*1024];
unsigned char chr;

testFile = fopen("testdata", "r");
start = clock();
    
while(!feof(testFile)) {
    chr = (unsigned char)getc(testFile);
}

end = clock();
printf("fgetc() %f seconds\n", ((float)end - (float)start) / (float)CLOCKS_PER_SEC);

fgets()

clock_t start;
clock_t end;
FILE* testFile;
char buf[8*1024];

testFile = fopen("testdata", "r"); 
start = clock();

do {}  while (fgets (buf, sizeof(buf), testFile) != NULL);

end = clock();
printf("fgets() %f seconds\n", ((float)end - (float)start) / (float)CLOCKS_PER_SEC);

fread()

clock_t start;
clock_t end;
FILE* testFile;
char data[8*1024];
int r = 1;

testFile = fopen("testdata", "r");
start = clock();
    
while(r!=0) {
    r = fread(data, sizeof(char), 1024*8, testFile);
}
    
end = clock();
printf("fread() %f seconds\n", ((float)end - (float)start) / (float)CLOCKS_PER_SEC);

Moving to WordPress 2.5

Thursday, April 10th, 2008

One of reasons I used to use simplyphpblog is that I could easily copy the blog from stokebloke to my home PC where I normally have a mirror running.

I use Unison to mirror my hosted site to my home PC. Its similar to rsync but provides a nice UI to check diffs and choose which way to propagate changes.

I have stayed away from using php with mysql because I didn’t know of an easy way to mirror the MySQL databases. Without the MySQL database working on both the hosted site and my home PC I would have trouble making changes without possibly breaking my hosted site. I never like making change on my hosted server, its always been better to check locally before uploading any changes.

I finally decided to use MySQL and configure it for wordpress. Setting up a new wordpress blog on my server was very easy. I exported the database from the hosted site and synchronised all the files to my home PC.

mysqldump --user=<user> --password=<password> --host=<hostname> <database> > <date>.sql

The only thing left then was getting the mysql database to work without changing any wordpress configurations, as I’d probably upload the changes to my hosted site and break that.

Lucky my hosted site used an alias for the MySQL server ‘remotemysqlhost’. I simply had to add this host to my PCs hosts file (making it resolve to localhost) and it would try to connect locally. I created the database with the same name, users,password etc. I then imported the SQL dump into the database.

bash> /usr/bin/mysqladmin -u root create %lt;database>
bash> /usr/bin/mysql -u root
mysql> connect <database>
mysql> GRANT ALL PRIVILEGES ON *.* TO '<username>'@'<host>' IDENTIFIED BY '<password>' with grant option;
mysql> exit;
bash> /usr/bin/mysql -u root <database> < <date>.sql

For some reason the wordpress pages kept jumping back to stokebloke.com. I figured out the wordpress needed to know about my localhost. To trick wordpress into using localhost for the links I just needed to patch the wp_options table locally. I.e

update wp_options set option_value = "http://localhost/wordpress"
where option_name = "home";
update wp_options set option_value = "http://localhost/wordpress"
where option_name = "siteurl";

Obviously if you want to access your wordpress blog from other PCs localhost should be replaced with the real PC name.

With that the database was working and I could then start to work on making a theme for wordpress so it appears like the rest of my site.

I still have an issue with the Tag Cloud widget using font sizes which are too large for my theme, but I decided to not use the tag cloud in the end.

UML tool in Netbeans is very slow

Thursday, April 10th, 2008

Well I finally decided to document some of the java classes in Model Manager.

I failed.

It appears Netbeans has a performance issue as the UML projects get larger the responsiveness of netbeans gets worse.

I had to submit this bug against Netbeans.

For some reason each click in Netbeans triggers a scanning of the XML document. As this document gets larger the scanning gets slower. Never mind the fact it only uses 1 of my 4 cores.

I looked into the actual UML project and the etd file is over 32 Mb. No wonder it takes a while to scan.

Edit: the issue has been fixed in Netbeans 6.1 beta version.