public void TestMute()
        {
            try
            {
                var rand = new Random();

                float[] orig = new float[1024];
                MutableSparseFloatArray fromEmpty = new MutableSparseFloatArray(new float[1024]);
			    float density = 0.2f;
			    int idx = 0;
			    while (rand.NextDouble() > density) {
				    idx++;
			    }
                int count = 0;
			    while (idx < orig.Length) 
                {
				    float val = (float)rand.NextDouble();
				    orig[idx] = val;
				    fromEmpty.Set(idx, val);
				    count++;
				    idx += 1;
				    while (rand.NextDouble() > density) {
					    idx++;
				    }
			    }

                float[] copy =new float[orig.Length]; 
                Array.Copy(orig, 0, copy, 0, orig.Length);
			    MutableSparseFloatArray fromPartial = new MutableSparseFloatArray(copy);

                // do 128 modifications
			    int mods = 128;
			    for (int i = 0; i < mods; i++) {
				    float val = (float)rand.NextDouble();
				    idx = rand.Next(orig.Length);
				    orig[idx] = val;
				    fromEmpty.Set(idx, val);
				    fromPartial.Set(idx, val);				
			    }

                for (int i = 0; i < orig.Length; i++)
                {
                    Assert.True(orig[i] == fromEmpty.Get(i), "orig " + orig[i] + " wasn't the same as fromEmpty " + fromEmpty.Get(i) + " at i=" + i);
                    Assert.True(orig[i] == fromPartial.Get(i), "orig " + orig[i] + " wasn't the same as fromPartial " + fromPartial.Get(i) + " at i=" + i);
                }

                Console.WriteLine("success!");
            }
            catch (Exception e)
            {
                Assert.Fail(e.ToString());
            }
        }
Esempio n. 2
0
        public void TestMute()
        {
            var rand = new Random();

            float[] orig = new float[1024];
            MutableSparseFloatArray fromEmpty = new MutableSparseFloatArray(new float[1024]);
            float density = 0.2f;
            int   idx     = 0;

            while (rand.NextDouble() > density)
            {
                idx++;
            }
            int count = 0;

            while (idx < orig.Length)
            {
                float val = (float)rand.NextDouble();
                orig[idx] = val;
                fromEmpty.Set(idx, val);
                count++;
                idx += 1;
                while (rand.NextDouble() > density)
                {
                    idx++;
                }
            }

            float[] copy = new float[orig.Length];
            Array.Copy(orig, 0, copy, 0, orig.Length);
            MutableSparseFloatArray fromPartial = new MutableSparseFloatArray(copy);

            // do 128 modifications
            int mods = 128;

            for (int i = 0; i < mods; i++)
            {
                float val = (float)rand.NextDouble();
                idx       = rand.Next(orig.Length);
                orig[idx] = val;
                fromEmpty.Set(idx, val);
                fromPartial.Set(idx, val);
            }

            for (int i = 0; i < orig.Length; i++)
            {
                Assert.True(orig[i] == fromEmpty.Get(i), "orig " + orig[i] + " wasn't the same as fromEmpty " + fromEmpty.Get(i) + " at i=" + i);
                Assert.True(orig[i] == fromPartial.Get(i), "orig " + orig[i] + " wasn't the same as fromPartial " + fromPartial.Get(i) + " at i=" + i);
            }

            Console.WriteLine("success!");
        }
Esempio n. 3
0
        public void TestSpeed()
        {
            Random r = new Random();

            float[] orig = new float[16 * 1024 * 1024];
            MutableSparseFloatArray arr = new MutableSparseFloatArray(new float[orig.Length]);

            for (int i = 0; i < 32 * 1024; i++)
            {
                int idx = r.Next(orig.Length);
                if (r.Next(1000) % 2 == 0)
                {
                    Assert.True(orig[idx] == arr.Get(idx), "orig " + orig[idx] + " not the same as arr " + arr.Get(idx) + " at idx=" + idx);
                }
                else
                {
                    float val = (float)r.NextDouble();
                    orig[idx] = val;
                    arr.Set(idx, val);
                }
            }

            // repeat it, but timed
            orig = new float[orig.Length];
            arr  = new MutableSparseFloatArray(new float[orig.Length]);
            int[]   idxs = new int[1024 * 1024];
            float[] vals = new float[idxs.Length];
            for (int i = 0; i < idxs.Length; i++)
            {
                idxs[i] = r.Next(orig.Length);
                vals[i] = (float)r.NextDouble();
            }

            long markTime = System.Environment.TickCount;

            for (int i = 0; i < idxs.Length; i++)
            {
                orig[i] = vals[i];
            }
            long elapsedTimePrim = System.Environment.TickCount - markTime;

            markTime = System.Environment.TickCount;
            for (int i = 0; i < idxs.Length; i++)
            {
                arr.Set(idxs[i], vals[i]);
            }
            long elapsedTimeMutable = System.Environment.TickCount - markTime;

            Console.WriteLine("elapsed time on the primitive array: " + elapsedTimePrim + "; elapsed time on the mutable condensed arr: " + elapsedTimeMutable);
            Console.WriteLine("ratio of time to do it on the mutable condensed arr, to time on primitive array: " + (double)elapsedTimeMutable / elapsedTimePrim);
        }
        public void TestSpeed()
        {
            Random r = new Random();

            float[] orig = new float[16 * 1024 * 1024];
            MutableSparseFloatArray arr = new MutableSparseFloatArray(new float[orig.Length]);

            for (int i = 0; i < 32 * 1024; i++)
            {
                int idx = r.Next(orig.Length);
                if (r.Next(1000) % 2 == 0)
                {
                    Assert.True(orig[idx] == arr.Get(idx), "orig " + orig[idx] + " not the same as arr " + arr.Get(idx) + " at idx=" + idx);
                }
                else
                {
                    float val = (float)r.NextDouble();
                    orig[idx] = val;
                    arr.Set(idx, val);
                }
            }

            // repeat it, but timed
            orig = new float[orig.Length];
            arr = new MutableSparseFloatArray(new float[orig.Length]);
            int[] idxs = new int[1024 * 1024];
            float[] vals = new float[idxs.Length];
            for (int i = 0; i < idxs.Length; i++)
            {
                idxs[i] = r.Next(orig.Length);
                vals[i] = (float)r.NextDouble();
            }

            long markTime = System.Environment.TickCount;
            for (int i = 0; i < idxs.Length; i++)
            {
                orig[i] = vals[i];
            }
            long elapsedTimePrim = System.Environment.TickCount - markTime;

            markTime = System.Environment.TickCount;
            for (int i = 0; i < idxs.Length; i++)
            {
                arr.Set(idxs[i], vals[i]);
            }
            long elapsedTimeMutable = System.Environment.TickCount - markTime;

            Console.WriteLine("elapsed time on the primitive array: " + elapsedTimePrim + "; elapsed time on the mutable condensed arr: " + elapsedTimeMutable);
            Console.WriteLine("ratio of time to do it on the mutable condensed arr, to time on primitive array: " + (double)elapsedTimeMutable / elapsedTimePrim);
        }