Thursday, October 27, 2016

Which Loop Runs Faster in NET?

Problem: Which Looping structure runs faster?

Selected Looping Structures :

  • While Loop
  • Do-While Loop
  • For Loop
  • Lamda For-Each Loop
  • Parallel For Loop
  • Parallel For-Each Loop

    Test Code : (Console Application)

    Main Method :
    static void Main(string[] args)
    {
     int[] someData = new int[] {465,545,674,354 };
       Stopwatch timer = new Stopwatch();
       timer.Start();
       ProcessUsingWhileLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using While Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingDoWhileLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using Do-While Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingForLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using For Loop :"+timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingLamdaForEachLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using Lamda ForEach Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingParallelForLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using Parallel For Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       timer.Start();
       ProcessUsingParallelForEachLoop(someData);
       timer.Stop();
       Console.WriteLine("Process Using Parallel ForEach Loop :" + timer.ElapsedMilliseconds);
       timer.Reset();
       Console.ReadLine();
     }

    ProcessUsingWhileLoop Method :
    static void ProcessUsingWhileLoop(int[] data)
    {
        int n = 0;
        while ( n < data.Length)
        {
            System.Threading.Thread.Sleep(data[n]);
            n++;
        }
    }
    

    ProcessUsingDoWhileLoop Method :
    static void ProcessUsingDoWhileLoop(int[] data)
    {
        int n = 0;
        do
        {
            System.Threading.Thread.Sleep(data[n]);
            n++;
        }
        while (n < data.Length);
    }
    

    ProcessUsingForLoop Method :
    static void ProcessUsingForLoop(int[] data)
    {
        for (int n = 0; n < data.Length; n++ )
        {
            System.Threading.Thread.Sleep(data[n]);
        }
    }
    

    ProcessUsingLamdaForEachLoop Method :
    static void ProcessUsingLamdaForEachLoop(int[] data)
    {
       data.ToList().ForEach(
            n => System.Threading.Thread.Sleep(n));
    }
    

    ProcessUsingParallelForLoop Method :
    static void ProcessUsingLamdaForEachLoop(int[] data)
    {
       Parallel.For(0, data.Length,
            n => 
            {
                System.Threading.Thread.Sleep(data[n]);
            });
    }
    

    ProcessUsingParallelForEachLoop Method :
    static void ProcessUsingParallelForEachLoop(int[] data)
    {
        Parallel.ForEach(data, 
             n =>
             {
                System.Threading.Thread.Sleep(data[n]);
             });
    }
    


    Results :
    (built-in Debug Mode and run from Visual Studio)

    Process Using While Loop: 2039
    Process Using Do-While Loop: 2041
    Process Using For Loop: 2064
    Process Using Lamda ForEach Loop: 2074
    Process Using Parallel For Loop: 984
    Process Using Parallel ForEach Loop: 870

    (built-in Release Mode ran from executable)
    Process Using While Loop: 2046
    Process Using Do-While Loop: 2040
    Process Using For Loop: 2039
    Process Using Lamda ForEach Loop: 2046
    Process Using Parallel For Loop: 838
    Process Using Parallel ForEach Loop: 837


    Conclusion :
    Based on the data collected, below is the list of the fastest looping structure to a slower one.
    1. Parallel ForEach Loop
    2. Parallel For Loop
    3. For Loop
    4. While Loop
    5. Do-While Loop
    6. Lamda ForEach

    Wednesday, October 26, 2016

    SAP Business One : Add more Demo DBs of Different Countries

    Problem: How to Add more Demo DBs of Different Countries for testing purposes?

    Solution: Need to install additional databases from the SAP Business One Server setup manually.

    Steps :
    1. Locate the Setup file in your SAP Business One installation folder.

    2. Select Modify, as you have already install at least one or more demo databases.

     3. Select the Country you want.

     4. Select the database server type.

    5. Provide database credentials to create the database.

    Wednesday, October 12, 2016

    NET Enterprise Services : COM, DCOM and COM+ Services


    It All begins with COM :

    Microsoft COM (Component Object Model) technology in the Microsoft Windows family of Operating Systems enables software components to communicate. COM is used by developers to create reusable software components, link components together to build applications, and take advantage of Windows services. COM objects can be created with a variety of programming languages. Object-oriented languages, such as C++, provide programming mechanisms that simplify the implementation of COM objects. The family of COM technologies includes COM+, Distributed COM (DCOM) and ActiveX® Controls.


    So What is COM+?

    COM+ is the name of the COM-based services and technologies first released in Windows 2000. COM+ brought together the technology of COM components and the application host of Microsoft Transaction Server (MTS). COM+ automatically handles programming tasks such as resource pooling, disconnected applications, event publication, and subscription and distributed transactions.


    COM+ and DCOM

    In order for Microsoft to provide developers with support for distributed transactions, resource pooling, disconnected applications, event publication and subscription, better memory and processor (thread) management, as well as to position Windows as an alternative to other enterprise-level operating systems, Microsoft introduced a technology called Microsoft Transaction Server (MTS) on Windows NT 4. With Windows 2000, that significant extension to COM was incorporated into the operating system (as opposed to the series of external tools provided by MTS) and renamed COM+.

    At the same time, Microsoft de-emphasized DCOM as a separate entity. Components that made use of COM+ services were handled more directly by the added layer of COM+, in particular by operating system support for interception. In the first release of MTS, the interception was tacked on - installing an MTS component would modify the Windows Registry to call the MTS software, and not the component directly. Windows 2000 also revised the Component Services control panel application used to configure COM+ components.