Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Sunday, December 21, 2008

SCMRSS reaches Alpha 1 release

SCMRSS is a simple web application that turns Source Control events into an RSS feed. Written in Ruby using the Ramaze web framework. Once configured the web server polls the source control repository for changes and when found delivers those changes as a simple RSS feed.

This is the first alpha release so I would welcome any and all feedback either here or as issues at RubyForge

Tuesday, April 29, 2008

Further perf ruby, python C++ file reading

Following on from the log files article I decided to do some basic perf checks of ruby and python reading text files. The results were a little disapointing - performance was roughly the same, so my ruby log file reading optimisation was complete rot.



Further experimentation required.































ARGV.each do | param |
cc = 0
File.new(param, 'r').each_line do |line|
cc += line.size
end
puts "File has #{cc} characters"
end

Processing /Users/gcb/work/log-analysis/cc.rb ... created /Users/gcb/work/log-analysis/cc.rb.html


Realy simple script - and probably the most obvious - add up the length of all the lines in the file.


File has 1673435763 characters

real 0m56.035s
user 0m33.873s
sys 0m3.609s





ARGV.each do | param |
cc = 0
i = File.open(param, "r")
begin
line = i.readline()
until line.nil?
cc += line.size
line = i.readline()
end
rescue Exception => e
ensure
i.close
end
puts "File has #{cc} characters"
end

Processing /Users/gcb/work/log-analysis/cc1.rb ... created /Users/gcb/work/log-analysis/cc1.rb.html


Based on previoud observations this one uses the realine method from the IO library but did not affect the performance.


File has 1673435763 characters

real 0m55.569s
user 0m35.506s
sys 0m3.451s




import sys
cc = 0

source = open(sys.argv[1])
for line in source:
cc += len(line)
source.close()
print 'file has ', cc, ' characters'

Processing /Users/gcb/work/log-analysis/cc.py ... created /Users/gcb/work/log-analysis/cc.py.html


As a benchmark a simple python scrpt - again adding up all the line lengths in the file.


file has 1673435763 characters

real 0m53.462s
user 0m23.147s
sys 0m3.781s




#include <stdio.h>


int main(int argc, char** argv)
{
int count = 0;
FILE* f = fopen(argv[1], "r");

while (getc(f))
count++;

printf("File has %d characters\n", count);
}

Processing /Users/gcb/work/log-analysis/cc.cpp ... created /Users/gcb/work/log-analysis/cc.cpp.html


Baseline written in C++


File has 1673392372 characters

real 0m53.167s
user 0m31.473s
sys 0m3.094s




#include <stdio.h>


int main(int argc, char** argv)
{
int count = 0;
FILE* f = fopen(argv[1], "r");

char buffer[512];
int read = fread(buffer, 1, 512, f);

while (read > 0) {
count += read;
read = fread(buffer, 1, 512, f);
}

printf("File has %d characters\n", count);
}

Processing /Users/gcb/work/log-analysis/cc1.cpp ... created /Users/gcb/work/log-analysis/cc1.cpp.html


A (poor) buffered version of the baseline written in C++


File has 1673435763 characters

real 0m52.425s
user 0m1.526s
sys 0m4.473s



Sunday, April 27, 2008

Blogging Code



Blogging Code


I quite often find myself blogging about program source code, that code is typically stored in source files which I then run through a pretty printer (something like source-highlight). Combining everyting together means some copy and pasting - not the most repeatable process and quite often the code and article evolve together - so I end up copying and pasting quite often.


So I came up with mashup. A small ruby program to process html files and handle include directives to do inline include of another file and for this purpose the results of a process


The following source was include with



<x:include value="source-highlight -o STDOUT ~/projects/mashup/mashup"/>

By running:



mashup blogging-code.html > blogging-code-publish.html


#!/usr/bin/ruby

ARGV.each do |arg|
contents = File.new(arg).read()

contents.sub!(/<s:include\s+value="([^"]*)"\s*\/>/) do |match|
replacement = File.new($1).read()
replacement.gsub!(/.*<body>/m, '')
replacement.gsub!(/<\/body>.*/m, '')
replacement
end

contents.sub!(/<x:include\s+value="([^"]*)"\s*\/>/) do |match|
replacement = `#{$1}`
replacement.gsub!(/.*<body>/m, '')
replacement.gsub!(/<\/body>.*/m, '')
replacement
end

puts contents
end



Saturday, March 1, 2008

Ruby initialise array and add in one step

I keep forgetting the syntax of this so perhaps this will help.

Given a hash where each value is an array of values this gives a nice concise way of setting things up (even if it is a little obscure).

map = {}

(map[:key] ||= []) << :value


The above results in a hash containing a key value of :key with an array containing :value