fletcher16 checksum

Benchmark created on


Preparation HTML

function getInput(data){
    var lines = data.split(/\n/);
    return lines;
  }

    function stringToByteArray(str){
        var arr = [],
        utf8;
        utf8 = unescape(encodeURIComponent(str));
        for (var i = 0; i < utf8.length; i++){
            arr.push(utf8.charCodeAt(i));
        }
        return arr;
    }

  function fletcHelp(str){
    var result = stringToByteArray(str);
    return fletcher16(result);
  }

  function fletcher16(arr){
    var sum1 = 0,
        sum2 = 0;
    for (var i = 0; i < arr.length; i++){
      sum1 = (sum1 + arr[i]) % 255;
      sum2 = (sum2 + sum1) % 255;
    }
    return ((sum2 << 8) | sum1).toString(16);
  }

  function compute(input){
    var data = getInput(input),
        result = [],
        newline = "\n";
    for (var i = 1; i < data.length; i++){
      result.push(i + " ");
      result.push(fletcHelp(data[i]));
      result.push(newline);
    }
    result.pop();
    return result.join("");
  }

Setup

var test = "8\nWhen you're parsing a file, which I was\n(and which programs do very often, probably more so than interactively\n prompting the user)it makes sense to include\n as part of the data. Also, \nif you plan on getting serious with C, you should look into malloc() and free(). They are essential if you are \nmaking programs with more than just the primitives. Run '$ man 3 malloc' on the command line to see the\n documentation for free(), malloc() and friends (that is, if you're on a UNIX system). \nThe man pages are an indispensable resource for C programming."

Test runner

Ready to run.

Testing in
TestOps/sec
MyTest
compute(test);
ready
test2
compute(test);
ready

Revisions

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