teste fors

Benchmark created on


Setup

var matrix,obj,i,j,len,index;
    
    matrix = new Array(1000*1000*4);
    len = matrix.length;
    obj = {0:255,1:0,2:255,3:255};
    
    function commonFor()
    {
        for (i = 0; i < lengthImgData; i+=4)
        {
                matrix[i] = obj[0];
                matrix[i+1] = obj[1];
                matrix[i+2] = obj[2];
                matrix[i+3] = obj[3];
        }
    }
    
    function nestedFor()
    {
        for(i = 0; i < 1000; i += 1)
        {
                for(j = 0; j < 1000; j += 1)
                {
                        index = (j + i * 1000) * 4;
                        matrix[index] = obj[0];
                        matrix[index+1] = obj[1];
                        matrix[index+2] = obj[2];
                        matrix[index+3] = obj[3];
                }
        }
    }
    
    function invertedcommonFor()
    {
        for (i = lengthImgData; i > -1; i-=4)
        {
                matrix[i] = obj[0];
                matrix[i+1] = obj[1];
                matrix[i+2] = obj[2];
                matrix[i+3] = obj[3];
        }
    }
    
    function invertednestedFor()
    {
        for(i = 1000; i > -1 ; i -= 1)
        {
                for(j = 1000; j > -1; j -= 1)
                {
                        index = (j + i * 1000) * 4;
                        matrix[index] = obj[0];
                        matrix[index+1] = obj[1];
                        matrix[index+2] = obj[2];
                        matrix[index+3] = obj[3];
                }
        }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
common for
commonFor();
ready
nested for
nestedFor();
ready
inverted common For
invertedcommonFor();
ready
inverted nested For
invertednestedFor();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.