Nick Parker
My ramblings on .NET...

Creating Objects - Round 3

Wednesday, 5 March 2008 18:08 by nickp

Ayende has been discussing the different ways of actually creating objects in .NET and the perf cost associated to each of them.  I thought I'd add to the mix one more method, using the DLR.  I've talked to several people who have identified concerns with the speed of the DLR so I found the results rather interesting.  The context is still the same, identify the time it takes to construct one million Created instances.

The delegate: 

delegate Created CreateInstance(int num, string name);

The structure:

    public class Created
    {
        public int Num;
        public string Name;

        public Created(int num, string name)
        {
            Num = num;
            Name = name;
        }
    }

 Grab the constructor:

ConstructorInfo ci = typeof (Created).GetConstructors()[0];

Define the parameters to be passed to the constructor (relative to the code block we are about to define):

Variable num = Variable.Parameter(SymbolTable.StringToId("num"), typeof (int));
Variable name = Variable.Parameter(SymbolTable.StringToId("name"), typeof (string));

Build a code block with the Ast factories for building expressions (this builds our function for creating new Created instances with our parameters):

CodeBlock block = Ast.CodeBlock("CreateInstance", typeof (Created),
                                  Ast.Return(Ast.New(ci, new Expression[] {Ast.Read(num), Ast.Read(name)})),
                                  new Variable[] {num, name}, new Variable[0]);

Compile our block:

CreateInstance create_instance = TreeCompiler.CompileBlock<CreateInstance>(block); 

 Invoke the compiled instance:

int iterations = 1000000;
Stopwatch watch = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
     create_instance(i, i.ToString());
}

The results are rather impressive, the run time on my machine was 00:00:00.2737688.  It looks like creating objects within the DLR via a dynamic code block is pretty cheap.  I've included the source here if you want to run the example.  The DLR bits are based off a release from CodePlex two days ago, the RubyForge bits are much older and will not compile with the above code.  Thoughts?

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   .NET | DLR | Open Source | Ruby | Software
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

IronRuby Emerges

Monday, 23 July 2007 17:57 by nickp

So John Lam annouced the alpha release of IronRuby, he mentions that they did a lot of work on string and arrays, unfortunately, with the current release .NET types aren't able to execute Ruby mixins. I was hoping to do something like the following:

require 'mscorlib' 
require 'System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'

list = System::Collections::ArrayList.new
list.Add(5)
list.Add(10)
list.Add(15)
list.extend Enumerable
list.each {|i| puts i.to_i}


The following code does execute:

require 'mscorlib'
require 'System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'
require 'System.Drawing, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'
require 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'

Application = System::Windows::Forms::Application
Form = System::Windows::Forms::Form
Button = System::Windows::Forms::Button
Point = System::Drawing::Point
Size = System::Drawing::Size
MessageBox = System::Windows::Forms::MessageBox
window = Form.new window.text = 'Testing IronRuby'
window.size = Size.new(250, 200)
button = Button.new
button.text = 'Click Me'
button.location = Point.new(75, 75)
button.click {|sender, e| MessageBox.show 'I was clicked'}
window.controls.add(button)
Application.EnableVisualStyles
Application.run(window)

Of all the windowing toolkits I've seen for Ruby, the .NET Framework is the easiest to read for me, although I'm sure toolkits like Swiby will get noticed, especially due to it's DSL syntax which is the new hotness in the development world these days. Next up, extending the IronRuby Builtins.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   .NET | Open Source | Ruby | Software
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Fun with Ruby

Friday, 16 December 2005 16:32 by nickp

Some of you may know that I like to play around with other programming languages, one of those being Ruby. I came across a t-shirt the other day and thought it was pretty funny. I modified it just a tad so you can run it from the console. Here it is:


class Programmer
def initialize(language)
@language
= language
end
def happy?
@language
=~ /ruby/ ? true : false
end
def make_happy!
puts @language
= 'Ruby'
end
end
you
= Programmer.new(gets)
unless you.happy?
you.make_happy!
end

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   Ruby
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Use Ruby to Unit Test C#

Wednesday, 5 January 2005 23:25 by nickp

As I mentioned earlier I would be posting an entry showing how to test your .NET code with Ruby. John Lam recently posted an example using Python.NET to test a C# class so I though I would use his C# example to show how you would do this with Ruby. First off, you will need the Ruby/.NET Bridge which is available here. Here is the C# class that we will be testing:

 

using System;
namespace Calc
{
public class Calc
{
public int Add(int x, int y)
{
return x + y;
}
public int Divide(int x, int y)
{
return x / y;
}
}
}

Next, compile that with the following command line:

 

csc /target:library Calc.cs

This will generate a Calc.dll file which you can load in your Ruby code as follows:

require 'dotnet'
require 'test/unit'
loadLibrary 'Calc'

class CalcTest < Test::Unit::TestCase
def test_divide
calc = Calc.new()
assert_equal(2, calc.Divide(8, 4))
end
def test_add
calc = Calc.new()
assert_equal(5, calc.Add(2, 3))
end
end

Run your code by calling "Ruby calctest.rb". Your output should be as follows:

 

>ruby testcalc.rb
Loaded suite testcalc
Started
..
Finished in 0.101 seconds.

2 tests, 2 assertions, 0 failures, 0 errors
>Exit code: 0

The test suite automatically runs all instance methods that start with 'test'. That's it, we can easily add or change new test cases in Ruby to test our Calc class that was written in C#.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   .NET | Ruby | TDD
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Microsoft Visual C# MVP Award

Wednesday, 5 January 2005 01:11 by nickp

I’ve been awarded the Visual C# MVP (Most Valuable Professional) award from Microsoft for 2005! I just received the email tonight with all the details regarding the program. This is a great honor, I was really delighted when I found out I had been selected. I am really looking forward to the MVP Global Summit; it will be a great chance to visit Seattle, friends and some family that live there.

I know I had mentioned that my first post of the New Year would reflect using the .NET Framework from Ruby, but I am currently working on an issue with overriding the WndProc within Ruby, I hope to go over this soon in an upcoming post. Wow, what a great way to start out the year!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   .NET | Ruby
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Linked on MSDN

Friday, 31 December 2004 16:23 by nickp
The other day I was playing with Google and did a check to see who is linking to me (this can be done by typing link:[yoursitehere] in Google) and I noticed that MSDN is linking to my blog in their Visual C# Developer Center page! Wow, that’s was amazing, and then I noticed that a good friend and former coworker of mine noticed (okay, I told him about it) and has posted about it on his blog. I feel compelled to create a trackback. To add, for those of you that have followed my posts where I have been using dynamically typed languages with .NET (i.e., IronPython), my first post in the new year will show an example of using Ruby with .NET, oh the fun, I can hardly wait! Wishing everyone a happy new year.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   .NET | Ruby
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed