[Mono-dev] Linq sample?

pablosantosluac pablosantosluac at terra.es
Tue Sep 25 12:52:45 EDT 2007


Oh... come on! I was so happy! :-(

Here is my code: it is just a dumb test. Note I commented out the 
"console.writeline" line.

I'm not using SQL, just searching in an in-memory array...

One question too, I'm totally new to linq so, is there a way to build the 
queries dynamically? I mean, maybe something like building them from a 
string that you can build at runtime or so??

pablo


using System;
using System.Linq;
using System.Collections.Generic;

namespace test
{

    class Revision
    {
        public long ObjId;
        public long RevNo;
        public long BranchId;
        public long ItemId;
        public bool Last;
        public string comment;
        public short Type;
        public long Size;
        public bool CheckedOut;
        public long ParentId = -1;
        public string Hash;
        public long Changeset;

        public Revision(long objid, long revno, long brid, long itemid, bool 
last,
            string comment, short type, long size, bool co, long parent, 
string hash,
            long cset)
        {
            this.ObjId = objid;
            this.RevNo = revno;
            this.BranchId = brid;
            this.ItemId = itemid;
            this.Last = last;
            this.comment = comment;
            this.Type = type;
            this.Size = size;
            this.CheckedOut = co;
            this.ParentId = parent;
            this.Hash = hash;
            this.Changeset = cset;
        }
    }


    class app
    {

        static void     Main(string[] args)
        {
            Revision[] revs = new Revision[int.Parse(args[0])];

            for( int i = 0; i < revs.Length; ++i )
            {
                revs[i] = new Revision(i, i % 4, i < 8000 ? 4 : i % 5, 0, i 
% 12 == 0,
                "this is a comment", 0, i, false, 1, 
"jakdjfalskjfasjfasjfasjf", 10);
            }

            int ini = Environment.TickCount;

            IEnumerable<Revision> query = from s in revs
                                where s.BranchId == 4 && s.Last
                                select s;

            int time = Environment.TickCount - ini;
            int num = 0;
            foreach( Revision r in query )
            {
//             Console.WriteLine("{0} - objid:{1}", num, r.ObjId);
               ++num;
            }
            Console.WriteLine("time retrieving {0} {1} ms", num, time);

            num = 0;
            ini = Environment.TickCount;
            foreach( Revision r in revs )
              if( r.Last && r.BranchId == 4)
                 ++num;

            Console.WriteLine("time retrieving iteratively {0} {1} ms", num, 
Environment.TickCount - ini);
        }
    }
}



----- Original Message ----- 
From: "Kamil Skalski" <kamil.skalski at gmail.com>
To: "pablosantosluac" <pablosantosluac at terra.es>
Cc: "Marek Safar" <marek.safar at seznam.cz>; "Robert Jordan" 
<robertj at gmx.net>; <mono-devel-list at lists.ximian.com>
Sent: Tuesday, September 25, 2007 5:04 PM
Subject: Re: [Mono-dev] Linq sample?


> 2007/9/25, pablosantosluac <pablosantosluac at terra.es>:
>> Hi,
>>
>> Well, I've just tried some simple tests yet, but I'd say performance is
>> awsome... I mean, searching in an array of 120 Million objects, using 
>> about
>> 1GB RAM looking for a simple query checking a couple of properties needed
>> only 185ms to finish!!!
>>
>> Doing it iteratively (looping through the array) took 284ms against 3 in 
>> a
>> 12Million array (linq is faster!).
>>
>>
>
> After getting such results I would guess there is some problem with
> the benchmark. You could post it here, so we can look at it. Such
> results are hardly possible, since linq is mostly just a layer about
> the standard search code, so it should never actually run faster
> (unless we speak about some complex Linq to SQL stuff involving code
> generation etc.).
>
> -- 
> Kamil Skalski
> http://nazgul.omega.pl 




More information about the Mono-devel-list mailing list