Wednesday, March 16, 2011

Latest browser javascript test result

This is a test for javascript to compare in different browsers. They are Firefox 4 RC, Internet Explorer 9, Opera 11 and Chrome 10.

I use web kit sun spider as tester in URL : http://www.webkit.org/perf/sunspider/sunspider.html

This is the result

Testee Firefox 4 RC IE 9 RTM Chrome 10 Opera 11
3d 47.5ms 41.5ms 57.3ms 46.2ms
    cube 20.3ms 17.1ms 16.6ms 13.0ms
    morph 9.2ms 6.2ms 17.1ms 18.7ms
    raytrace 18.0ms 18.2ms 23.6ms 14.5ms
         
  access 44.8ms 43.2ms 47.8ms 46.5ms
    binary-trees 7.0ms 6.2ms 2.9ms 2.8ms
    fannkuch 21.3ms 15.5ms 28.1ms 23.0ms
    nbody 8.1ms 17.1ms 10.9ms 13.2ms
    nsieve 8.4ms 4.4ms 5.9ms 7.5ms
         
  bitops 19.0ms 28.9ms 36.5ms 17.7ms
    3bit-bits-in-byte 1.0ms 1.9ms 3.7ms 1.7ms
    bits-in-byte 8.5ms 7.1ms 8.5ms 3.3ms
    bitwise-and 2.4ms 8.8ms 11.8ms 2.1ms
    nsieve-bits 7.1ms 11.1ms 12.5ms 10.6ms
         
  controlflow 3.4ms 3.0ms 4.9ms 3.9ms
    recursive 3.4ms 3.0ms 4.9ms 3.9ms
         
  crypto 20.2ms 18.6ms 29.8ms 20.5ms
    aes 11.3ms 7.1ms 12.6ms 12.1ms
    md5 4.9ms 5.4ms 7.8ms 4.5ms
    sha1 4.0ms 6.1ms 9.4ms 3.9ms
         
  date 45.9ms 29.0ms 37.0ms 36.0ms
    format-tofte 23.9ms 11.1ms 17.2ms 15.7ms
    format-xparb 22.0ms 17.9ms 19.8ms 20.3ms
         
  math 27.7ms 26.0ms 28.7ms 37.6ms
    cordic 9.2ms 1.0ms 5.3ms 8.2ms
    partial-sums 13.2ms 18.4ms 18.2ms 24.8ms
    spectral-norm 5.3ms 6.6ms 5.2ms 4.6ms
         
  regexp 17.2ms 10.9ms 14.5ms 14.6ms
    dna 17.2ms 10.9ms 14.5ms 14.6ms
         
  string 88.9ms 93.2ms 104.4ms 108.9ms
    base64 5.8ms 5.8ms 8.2ms 11.9ms
    fasta 14.0ms 23.8ms 18.9ms 21.0ms
    tagcloud 28.0ms 29.6ms 25.1ms 31.3ms
    unpack-code 30.0ms 20.5ms 34.4ms 22.8ms
    validate-input 11.1ms 13.5ms 17.8ms 21.9ms

 

 

Tuesday, January 12, 2010

Choose : Parallel or not in .NET 4.0 beta 2; Part 1

This is a little sample to choose parallel or not. It is used LINQ to object.

Example of source code:


DateTime start = DateTime.MinValue;
DateTime stop = DateTime.MinValue;
TimeSpan seleisih;
Console.WriteLine("Sequential");

IEnumerable<int> cobaAss = Enumerable.Range(0, 30000000);
var hh = from x in cobaAss
         where x % 3 == 0
         select Math.Sqrt(x);
start = DateTime.Now;
foreach (var item in hh)
{
    double x = item * 1.5;
}
stop = DateTime.Now;
seleisih = stop - start;

Console.WriteLine("Start : {0}", start);
Console.WriteLine("Stop : {0}", stop);
Console.WriteLine("Selisih (tick) : {0}", seleisih.Ticks);

// --

Console.WriteLine("Parallel");

cobaAss = Enumerable.Range(0, 30000000);
hh = from x in cobaAss.AsParallel()
     where x % 3 == 0
     select Math.Sqrt(x);
start = DateTime.Now;
//Parallel.ForEach<double>(hh, (item) => { });
foreach (var item in hh)
{
    double x = item * 1.5;
}
stop = DateTime.Now;
seleisih = stop - start;

Console.WriteLine("Start : {0}", start);
Console.WriteLine("Stop : {0}", stop);
Console.WriteLine("Selisih (tick) : {0}", seleisih.Ticks);

Console.WriteLine("Parallel Doit manually");

var cobaAss1 = Enumerable.Range(0, 10000000);
var cobaAss2 = Enumerable.Range(10000000, 10000000);
var cobaAss3 = Enumerable.Range(20000000, 10000000);

var hh1 = from x in cobaAss
          where x % 3 == 0
          select Math.Sqrt(x);
var hh2 = from x in cobaAss
          where x % 3 == 0
          select Math.Sqrt(x);
var hh3 = from x in cobaAss
          where x % 3 == 0
          select Math.Sqrt(x);
start = DateTime.Now;
Parallel.Invoke(
    () =>
    {
        foreach (var item in hh1)
        {
            double x = item * 1.5;
        }
    },
    () =>
    {
        foreach (var item in hh2)
        {
            double x = item * 1.5;
        }
    },
    () =>
    {
        foreach (var item in hh3)
        {
            double x = item * 1.5;
        }
    });
stop = DateTime.Now;
seleisih = stop - start;

Console.WriteLine("Start : {0}", start);
Console.WriteLine("Stop : {0}", stop);
Console.WriteLine("Selisih (tick) : {0}", seleisih.Ticks);

Console.ReadLine();


I use console project to use it. You can try your own test

Below is the result:

test_parallel

This is strange since sequential is faster than parallel using AsParallel() or manually to do in parallel.

Summary: Parallelism is not always fasten the query

Sunday, January 10, 2010

Forcing parallelism in .NET 4 b2

Concurrent and parallel programming is really hard to develop. In the old days like .NET 3.x, 2.0 or older, developer can obtain parallel by creating a new thread, maintain their own thread manually; that can harm system performance and execution when not managed carefully.

The past approach (.NET 2.x-3.x)

public static void DoSomething()
{
      // do something hard and time consuming like counting with loop
      while(true)
      {
             int x = 3 + 5;
       }
}

public static void Run()
{
      Thread t = new Thread(new ThreadStart(DoSomething));
      t.Start();
      t.Join();
      // do whatever
}

In the .NET 4.0 (right now I use beta 2), It is easier to develop by using Invoke method from Parallel class
public static void Run()
{
      Parallel.Invoke(()=>{ DoSomething(); });
}

Code above will pause main thread and run all code inside Invoke until all running thread terminated. This method deliver more safety than manual ones.

Developer can easily set processor affinity for specific thread by assigning them into the methods. Processor affinity is a bit flags. The documentation is available on microsoft MSDN on class Process with property ProcessorAffinity.

For example, the thread will be executed on second processor, set affinity by

public static void Run()
{
      Parallel.Invoke(()=>{
              Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)2;
              DoSomething(); }
      );
}

In task manager. the process will make second processor runs high.

cpu_sibuk

Sunday, November 15, 2009

When to use F#.NET and C#.NET/VB.NET

F# is a new development language that is available on Visual Studio 2010; It can be downloaded separately from Microsoft research F# for Visual Studio 2008. It is a new language that has paradigm of functional programming. It is differs from C# or VB that has paradigm of imperative by design.

Functional means preventing state and mutable data, and treats as mathematic functional.  This is in contrast of imperative that can change state using statement.

In a software development project, F# is use on financial or math. Some banking applications for example needs financial project like counting interest, pmt, value manipulation or else. C#/VB is useful on UI interaction, data/io interaction.

Make IEnumerable<T> to BindingList<T>as datasource for collection control

IEnumerable<T> can be used as datasource for some controls especially on collection thing like gridview or else. It happens when assigning Datasource property from the control with data in it.

for example:

IEnumerable<Sales> sales = GetAllSales(); // return value from GetSales is IEnumerable<Sales>
GridView1.DataSource = sales;
GridView1.DataBind();

The samples above will display all sales from methods. This is good when there is no paging in GridView. For advanced use like paging or else; Try to cast or convert to BindingList<T> so it can be use in the control.

public static BindingList<T> ToBindingList<T>(this IEnumerable<T> data)
{
    BindingList<T> result = null;
    if (data != null)
    {
        result = new BindingList<T>();
        foreach (T item in data)
        {
            result.Add(item);
        }
    }
    return result;
}

The method above will help to convert to BindingList<T> and ready to use as datasource in control

Sunday, April 26, 2009

Internet Explorer 8 still not supported DOM 2 Events

I am surprised that until today, Internet Explorer 8 is not supporting DOM 2 Events. This can be tested using method that is described on DOM 2 Events like addEventListener.

I try to make a very simple sample like in Javascript:

<script language="javascript" type="text/javascript">
var btn = document.getElementById('btnShow');
btn.addEventListener('click', function() {
alert('DOM 2 Alert A');
}, false);
btn.addEventListener('click', function() {
alert('DOM 2 Alert B');
}, false);

</script>


Code above in Firefox 3.0.8 and Google Chrome 1.0 work perfectly. It will register functions that will executed when the button click. It will display dialog box twice. I try in Internet Explorer 7 and 8. It says that object doesn;t have property or methods.



Internet Explorer have methods that similar to DOM 2 addEventListener; that is called attachEvent. This methods work the same as addEventListener by registering many functions that is executed when event triggers. The only different is attachEvent register the functions in LIFO while addEventListener register in FIFO.



Below is the simple complete html + javascript:



<script language="javascript" type="text/javascript">
var btn = document.getElementById('btnShow');
var browserName = navigator.appName;

if (browserName == 'Microsoft Internet Explorer') {
btn.attachEvent('onclick', function() {
alert('NON DOM 2 Alert A');
});
btn.attachEvent('onclick', function() {
alert('NON DOM 2 Alert B');
}
);
}
else {
btn.addEventListener('click', function() {
alert('DOM 2 Alert A');
}, false);
btn.addEventListener('click', function() {
alert('DOM 2 Alert B');
}, false);
}

</script>


In the Internet Explorer, dialog with NON DOM 2 Alert B will appear first and then NON DOM 2 Alert A because attachEvent is registering in LIFO



In the Firefox and Google Chrome, dialog with DOM 2 Alert A will appear first then DOM 2 Alert B because attachEvent is registering in FIFO



Technorati Tags: ,,

Friday, February 20, 2009

Checking .NET framework installed using ASP.NET

Some articles in codeproject or other site help me on checking the version of .NET Framework installed on a machine. They use techniques to get information from various resources such as Registry or even look at the windows folder. You can see the articles on :

Code Project  xFxDetection : http://www.codeproject.com/KB/dotnet/XFxDetect.aspx

The article above show about searching keys and values in specific location on registry. It can detect version including RTM and Service pack.

This is good for desktop application and not for web application. In web application, most of system administrator prevent access specific resource like registry. So I came with an idea of checking some features that is available in that machine with specific assembly.

First thing is about assembly that come with .NET Framework 2.0. We can easily choose any assembly that come from .NET Framework for example is mscorlib.dll. This is a basic assembly that must be existed. I just is it available by load that assembly into current application domain. Other assembly can be loaded with the same way.

In .NET Framework 3.0, There are 3 pillars of the Framework; There are Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), Windows Workflow Foundation (WF). Those all pillars come with new assembly. I choose PresentationCore as an assembly to load in my current application domain. That assembly belongs to WPF.

In .NET Framework 3.5, There are new functionality like LINQ and ASP.NET 3.5 Ajax. I choose System.Core since this assembly contain LINQ

In .NET Framework 3.5 with Service Pack 1, There is functionality like web routing.  I choose System.Web.Abstraction to load because it belongs to web routing techniques.

The technique is really simple. I just load all that assembly with their culture, version and token; into my current application domain. If this process throw exception, the machine is not supporting that framework version.

The code below is a sample. I use an aspx without code behind so user can reuse and put on web server easily.

I give a name of this file : TestedFramework.aspx


 

<%@ Page Language="C#" AutoEventWireup="true"  %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/C#" runat="server">
        public const string Net20KnownAssembly = "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
        public const string Net30KnownAssembly = "PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
        public const string Net35KnownAssembly = "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
        public const string Net35Sp1KnownAssembly = "System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35";

        public const string ResponseFormat = ".Net Version {0} is {1}";
        public enum ServerNetVersionInstalled
        {
            // not implemented
            Net1x,
            Net20,
            Net30,
            Net35,
            Net35SP1,
        }

        public IEnumerable<ServerNetVersionInstalled> GetInstalledVersion()
        {
            List<ServerNetVersionInstalled> lstVersion = new List<ServerNetVersionInstalled>();
            // try for .NET 2.0
            try
            {
                AppDomain.CurrentDomain.Load(testedframework_aspx.Net20KnownAssembly);
                lstVersion.Add(ServerNetVersionInstalled.Net20);
            }
            catch { }

            // try for .NET 3.0
            try
            {
                AppDomain.CurrentDomain.Load(testedframework_aspx.Net30KnownAssembly);
                lstVersion.Add(ServerNetVersionInstalled.Net30);
            }
            catch { }

            // try for .NET 3.5
            try
            {
                AppDomain.CurrentDomain.Load(testedframework_aspx.Net35KnownAssembly);
                lstVersion.Add(ServerNetVersionInstalled.Net35);
            }
            catch { }

            // try for .NET 3.5 Sp1
            try
            {
                AppDomain.CurrentDomain.Load(testedframework_aspx.Net35Sp1KnownAssembly);
                lstVersion.Add(ServerNetVersionInstalled.Net35SP1);
            }
            catch { }
            // try for .NET 1x
            // not implemented
            return lstVersion;
        }
    </script>
    <style media="screen" type="text/css">
        em {color:Red; font-weight:lighter; font-size:large;}
        strong {color:Blue; font-weight:bolder; font-size:large;}
    </style>
</head>
<body style="font-family:Arial;">
    <form id="form1" runat="server">
    <div style="height:100px; background-color:#66CCFF; vertical-align:middle; text-align:center;">
        <h1>List of Framework Version Installed</h1>
    </div>
    <hr />
    <div style="background-color:#CEE9F0;">
        <%
            List<ServerNetVersionInstalled> lstVersion = new List<ServerNetVersionInstalled>(this.GetInstalledVersion());
            List<string> lstResponse = new List<string>();
            string formated = "";
            // .NET 2.0
            if (lstVersion.IndexOf(ServerNetVersionInstalled.Net20) >= 0)
            {
                formated = string.Format(testedframework_aspx.ResponseFormat, "2.0", "<strong>Installed</strong>");
            }
            else
            {
                formated = string.Format(testedframework_aspx.ResponseFormat, "2.0", "<em>Not Installed</em>");
            }
            lstResponse.Add(formated);

            // .NET 3.0
            if (lstVersion.IndexOf(ServerNetVersionInstalled.Net30) >= 0)
            {
                formated = string.Format(testedframework_aspx.ResponseFormat, "3.0", "<strong>Installed</strong>");
            }
            else
            {
                formated = string.Format(testedframework_aspx.ResponseFormat, "3.0", "<em>Not Installed</em>");
            }
            lstResponse.Add(formated);

            // .NET 3.5
            if (lstVersion.IndexOf(ServerNetVersionInstalled.Net35) >= 0)
            {
                formated = string.Format(testedframework_aspx.ResponseFormat, "3.5", "<strong>Installed</strong>");
            }
            else
            {
                formated = string.Format(testedframework_aspx.ResponseFormat, "3.5", "<em>Not Installed</em>");
            }
            lstResponse.Add(formated);

            // .NET 3.5 SP 1
            if (lstVersion.IndexOf(ServerNetVersionInstalled.Net35SP1) >= 0)
            {

                formated = string.Format(testedframework_aspx.ResponseFormat, "3.5 SP 1", "<strong>Installed</strong>");
            }
            else
            {
                formated = string.Format(testedframework_aspx.ResponseFormat, "3.5 SP 1", "<em>Not Installed</em>");
            }
            lstResponse.Add(formated);

            this.Response.Write("");
            foreach (string item in lstResponse)
            {
                string html = string.Format("<p>{0}</p>",item);
                this.Response.Write(html);   
            }
             %>
    </div>
    </form>
</body>
</html>