Iterating items in a list - for vs. while vs. foreach

When iterating over the items in list in C# you have at least 3 options as to how you want to do it. This blog post is simply a small journey into exploring the 3 options for, foreach and while seen from a performance perspective.

 

For this journey I will take a look at the following very simple console application

using System;
using System.Diagnostics;
using System.Linq;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var iterations = 10000000;
            var test = 0;

            for (var a = 0; a < 50; a++)
            {
                var list1 = Enumerable.Range(0, iterations).ToList();
                var list2 = Enumerable.Range(0, iterations).ToList();
                var list3 = Enumerable.Range(0, iterations).ToList();


                var st = new Stopwatch();
                st.Start();
                for (var i = 0; i < list3.Count; i++)
                    test = list3[i];
                st.Stop();

                var st2 = new Stopwatch();
                st2.Start();
                foreach (var i in list2)
                    test = i;
                st2.Stop();


                var st3 = new Stopwatch();
                var iterationcount = iterations;
                st3.Start();
                while (iterationcount-- > 0)
                    test = list1[iterationcount];
                st3.Stop();

                Console.WriteLine("1: [{0}] 2: [{1}] 3: [{2}]", st.ElapsedMilliseconds,
                    st2.ElapsedMilliseconds, st3.ElapsedMilliseconds);
            }
        }
    }
}

This is a very simple test case just to prove a point

I have a loop where I do 50 test runs and for each test run for each type of loop I create a sample list of 10.000.000 integers and then I simply test each type of loop to see which loop type will give me the fastest access to all my numbers again.

 

Before doing the test I would consider them more or less equal and I would actual expect the foreach loop to be the fastest because it does not have to continuously index the list. But having run the code multiple times (and even having rearranged the order of the loops), the result always come out similar and looking somewhat like this

Loop performance

 

So most of the time the for-loop is the slowest. The foreach-loop is in second and then the while-loop is actually the fastest.

 

Off course your mileage may vary depending on what you are trying to do, but if you need to squeeze out the last milliseconds in your app, you can consider which kind of loops that you are using.

Øredev - a conference with an attitude

This Wednesday I showed up for my first Øredev conference in Malmö.

I was prepared for a technical conference where I was supposed to learn some new tricks to improve my development workflow and make me more productive and efficient.

What I got from the conference though was as expected but also much, much more.

The conference was set for 3 days and throughout the conference there had been set a scene and there was a red line for the days. The theme this year was Computer Power. Within that theme was both the power in the computers and devices that we have, but also the power that we as developers have utilizing computers.

Going through the days the talk went from

- The state of world today

- The possibilities of the future

- The actions we can take to ensure that we get the future that we want

The focus was that if we, the developers, do not take action now, and develop in an ethical manner, the future might not look so bright.

Furthermore we need to start voicing our concerns for the current politics and ensure that the government and corporations start acting ethically and respectfully and ensure our privacy.

So now is the time to start making some noise.

Using Sharepoint AuthoringContainer on pages with Url Extension

This blog post is based on a real life issue, that we experienced on our site once.

 

In the header of out Sharepoint masterpage we had inserted 2 AuthoringContainer controls with a plan for adding two different stylesheets to the page. One stylesheet used for Reader and another for Editors. This seemed pretty straightforward to achieve using the DisplayAudience property on the AuthonringContainer.

It can have the 2 values

  • AuthorsOnly
  • ReadersOnly

So adding two authoring containers and setting that property to match each scenario seemed like the right way to go, and everything worked as expected.

 

 

The one day we received information that the page looked strange in certain scenarios for some pages. After some debugging we found a common pattern for the urls, as the urls that not displaying correctly looked like this

http://domain/site/pages/default.aspx/moreinfo

http://domain/site2/pages/default.aspx/info

 

The patthern for these urls is that they are in a format the ends in /filename.fileextension/urlextension. This last part of the url can be retrieved in .NET using HttpRequest.PathInfo and as such it is a known format. The problem with this format is that whenever something is added to the url like this Sharepoint is not able to properly set SPContext.Current.ListItem and by default the AuthoringContainer is not displayed if there is no current listitem.

 

The solution:

Very simply you can set the property RequireListItem to false and the it works as expected, so the tag looks like this

<PublishingWebControls:AuthoringContainer ID="AuthoringContainer1" DisplayAudience="ReadersOnly" RequireListItem="false"
        runat="server">

A matter of scopes (method paramaters versus class properties)

Defining variable names must be done carefully sometimes to prevent logical errors in your code. Just to provide and example of where things can go wrong you can see the following snippet.

 

public class TestClass
    {
        public int Variable { get { return -1; } }
        private int _variable1;
        private int _variable2;
        public TestClass(int Variable)
        {
            _variable1 = Variable;
            _variable2 = this.Variable;
        }

        public override string ToString()
        {
            return "Value of variable1: \{_variable1}\{Environment.NewLine}Value of variable2: \{_variable2}";
        }
    }

 

This example is pretty straightforward and easy to correct, but can be harder to detect in a larger code base.

 

But the lesson is to be careful when naming your variables to ensure that you know exactly what variables are used at which times.

In the above example the method parameter is more locally scoped and is thus prioritized rather then the class property. And in order to use the class property you need in this case to use the "this" keyword.

Sjov oversættelse - Sharepoint 2007

Jeg har fuld forståelse for at det kan være svært at oversætte software og de ledetekster der er i en softwareløsning. Men det er alligevel meget sjovt når man støder på nogle mærkelige tekster.

 

F.eks. denne dialog som dukker op i Sharepoint 2007 når jeg prøver at slette et dokument fra et dokumentbibliotek.

 

Men svaret er "Nej, jeg vil gerne slette dem,  ikke genbruge dem" :-)

Funny thing – Sharepoint 2010 - NotImplementedException

During a debugging session I was just poking around some Sharepoint dll's using Reflector.

And then I stumbled upon this little snippet. I must say, I didn't expect to find this exception being thrown in a released Microsoft product.

It does implement an interface, so I presume it must make sense somehow, I was just surprised to see it.

 

 

Happy coding

Variable scopes in C# - Beware of naming similiarities

Just the other day I came across a sample of code with some similar named variables, where the flow did not work probably. It turned out to be an issue of some locale variables which were named the same as some global variables.

This made me think some about it and do some testing about scoping, and I created this small snippet to illustrate what can be done and what cannot be done

using System;
namespace ScopeTest
{
    class Program
    {
        static void Main(string[] args)
        { new Program().Debug(); }
 
        int temp = 1;
        void Debug()
        {
            int temp = 2;
            Console.WriteLine(temp); //Prints 2
            Console.WriteLine(this.temp); //Prints 1
            //Action a = delegate() //Will not compile
            //{
            //    int temp = 3;
            //    Console.WriteLine(temp);
            //};
            //a();
        }
    }
}

 

This is just something you need to know and be aware of otherwise you might experience strange unexpected issues.

Updating Sharepoint menu to display itemcount in menu

I just though I would share this small snippet I created which can be added to a Sharepoint page on a website, and then when called it will iterate through the leftmenu and for links to folders/list in the current site it will add a counter indicating the number of items in the given list.

It is very simple and naïve in its implementation, but I think the concept is pretty neat

 

function ShowFolderCount() {
    var ctx = SP.ClientContext.get_current();
    // point  to get all the lists  
    var myLists = ctx.get_web().get_lists();
    // Load all lists 
    var listinfoarray = ctx.loadQuery(myLists, "Include(Title, ItemCount, RootFolder)");
    // async excute 
    ctx.executeQueryAsync(function () {
        var listinfo = new Array();
        // iterate to all items 
        for (var i = 0; i < listinfoarray.length; i++) {
            var listdata = new Object();
            var currentItem = listinfoarray[i];
            listdata.title = currentItem.get_title();
            listdata.count = currentItem.get_itemCount();
            listdata.url = currentItem.get_rootFolder().get_serverRelativeUrl();
            listinfo.push(listdata);
        }
        var submenus = jQuery(".ms-quickLaunch .menu .root ul.static");
        submenus.each(function () {
            var menu = jQuery(this);
            var links = menu.find("a");
            links.each(function () {

                try{
                    var link = jQuery(this);
                    var url = unescape(link.attr("href"));
                    for (var t = 0; t < listinfo.length; t++) {
                        var c = listinfo[t];
                        if (url && url.startsWith(c.url)) {
                            link.find("span span").text(c.title + " (" + c.count + ")");
                            break;
                        }
                    }
                }
                catch(ex)
                {}
            }
            );
        }
        );

    }, function () { });
}

Developing JavaScript solutions with remote data using Chrome

When developing simple JavaScript applications using data i.e. from a SharePoint site using SPservices, then you will be challenged with warnings about cross-site scripting if you try to create the files locally on your own machine before publishing the files to SharePoint.

This is usually a good thing and is meant to keep browser users safe, but what if you just want to develop a simple html page with some JavaScript locally with intentions of later uploading the files to a SharePoint document library to run it from there. Then it could be nice to use live data and avoid having to install SharePoint locally just to test with.

Here comes Google Chrome to the rescue. When starting Google Chrome you can start it with a flag called --disable-web-security (which is asking for problems under normal circumstances)

I.e. you can create a shortcut like this

 

When Chrome is started, you will see this warning indicating that you are on your own, but it really simplifies development using SharePoint resources.