Activity Stream
48,167 MEMBERS
6951 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 3 of 3 FirstFirst 123
Results 21 to 23 of 23
  1.     
    #21
    Respected Developer
    The express edition is free.

    Made a few optimizations yesterday:
    PHP Code: 
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Text;

    namespace 
    Hyperz.RGen
    {
        public 
    unsafe class RGen
        
    {
            private 
    uintpSeedX;
            private 
    uintpSeedY;

            public 
    uint SeedX
            
    {
                
    get { return *pSeedX; }
                
    set { *pSeedX value; }
            }

            public 
    uint SeedY
            
    {
                
    get { return *pSeedY; }
                
    set { *pSeedY value; }
            }

            public 
    RGen(uint seed)
            {
                
    uintpX stackalloc uint[1];
                
    uintpY stackalloc uint[1];

                
    pSeedX pX;
                
    pSeedY pY;

                
    this.SeedX seed;
                
    this.SeedY 362436069;
            }

            public 
    RGen(int seed) : this((uint)seed) { }

            public 
    RGen() : this(Environment.TickCount) { }

            public 
    uint Generate()
            {
                *
    pSeedX 36969 * (*pSeedX 65535) + (*pSeedX >> 16);
                *
    pSeedY 18000 * (*pSeedY 65535) + (*pSeedY >> 16);

                return (*
    pSeedX << 16) + (*pSeedY 65535);
            }

            private 
    void GenerateAt(uintpDest)
            {
                *
    pSeedX 36969 * (*pSeedX 65535) + (*pSeedX >> 16);
                *
    pSeedY 18000 * (*pSeedY 65535) + (*pSeedY >> 16);

                *
    pDest = (*pSeedX << 16) + (*pSeedY 65535);
            }

            public 
    uint[] GenerateMultiple(int amountint threadLimit 32)
            {
                if (
    threadLimit == || amount threadLimit)
                {
                    
    uint[] results = new uint[amount];

                    
    fixed (uintpR results)
                    {
                        for (
    uintpRpR amounti++) *this.Generate();
                    }

                    return 
    results;
                }
                else
                {
                    
    int amountPerInstance amount threadLimit;
                    
    int addToLast amount - (amountPerInstance threadLimit);
                    
    bool addOneToLast = (amount 1) == 1;
                    var 
    po = new ParallelOptions() { MaxDegreeOfParallelism threadLimit };

                    
    uint[] results = new uint[amount];

                    
    Parallel.For(0threadLimitponum =>
                    {
                        var 
    = new RGen(this.Generate());
                        
    int count = (addOneToLast && num == threadLimit 1) ?
                            
    amountPerInstance addToLast amountPerInstance;

                        
    uint[] numbers = new uint[count];

                        
    fixed (uintpN numbers)
                        {
                            for (
    uintpNpN counti++) *r.Generate();
                        }

                        
    fixed (uintpN numberspR results)
                        {
                            
    uintpn pN;
                            
    uintpr pR + (num amountPerInstance);
                            
    uintcnt pn count;

                            for (; 
    pn cntpn++, pr++) *pr = *pn;
                        }
                    });

                    return 
    results;
                }
            }


            
    // OOOOLLLLLLDDDDDD
            /*public uint[] GenerateMultiple(int amount, int threadLimit = 32)
            {
                if (amount < threadLimit) threadLimit = 1;

                int amountPerInstance = amount / threadLimit;
                int addToLast = amount - (amountPerInstance * threadLimit);
                bool addOneToLast = (amount & 1) == 1;
                var po = new ParallelOptions() { MaxDegreeOfParallelism = threadLimit };

                uint[] results = new uint[amount];

                Parallel.For(0, threadLimit, po, num =>
                {
                    var r = new RGen(this.Generate());
                    int count = (addOneToLast && num == threadLimit - 1) ?
                        amountPerInstance + addToLast : amountPerInstance;

                    uint[] numbers = new uint[count];

                    for (int i = 0; i < count; i++) numbers[i] = r.Generate();

                    lock (results) numbers.CopyTo(results, num * amountPerInstance);
                });

                return results;
            }*/
        
    }

    Made the 2 seed properties pointers rather than values. The memory for those seed properties is now allocated on the stack rather than on the heap (faster). Removed the address locking from the Generate function. And finally optimized the GenerateMultiple function a bit.

    Single thread: 0.69 seconds
    4 threads: 0.38 seconds

    I think this is as fast as I'll get this algorithm with C# .

  2.     
    #22
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com
    you win man. over my head

  3.   Sponsored Links

  4.     
    #23
    Member
    Nice work Hyperz

Page 3 of 3 FirstFirst 123

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. A Google +-challenge?
    By Daniel in forum General Discussion
    Replies: 2
    Last Post: 17th Jul 2011, 08:30 PM
  2. Challenge to GFX Genius !
    By softleaks in forum Graphics Area
    Replies: 21
    Last Post: 28th May 2011, 09:01 AM
  3. [c#] CHALLENGE - Beat the time!
    By jayfella in forum Web Development Area
    Replies: 18
    Last Post: 24th Jun 2010, 07:38 PM
  4. I Challenge U ALL
    By Chief-Editor in forum General Discussion
    Replies: 19
    Last Post: 2nd Mar 2010, 08:46 AM

Tags for this Thread

BE SOCIAL