Ad
  • Custom User Avatar

    Very glad you succeeded but for the future don't post issue before you are quite sure of your program:-) You can simply post a "Question". You can label your post with "Issue", "Question" or "Suggestion". BTW thanks for your last feedback!

  • Custom User Avatar

    Lots of guys passed the tests in C#. I just tried a few solutions and everything worked fine. The tests are waiting for a pair or null; here it says you that you failed at index 0 of an array.
    Please if you fail a test could you post the complete result of this test with the input and your output?

  • Custom User Avatar

    That's interesting! I'm afraid we're entering engine-specific optimization territory, though.

    Specifying the array dimensions is generally accepted as a hint for the compiler. It may or may not optimize for it. It doesn't mean it'll pre-allocate anything. It's also highly dependent on the later usage of the array. In my system, Safari used it for some slight advantage on your jsperf, while Chrome didn't.

    It's "hard" to implement the "fastest" version of an algorithm in js. Each engine has its own optimizations and, on top of that, each one of them keeps updating their speed-focused tweaks, often rendering previous hacks slower than naive implementations.

    Chrome did account for it on smaller arrays, though (I'm not sure what kind of buffer you speak of, but even when making several arrays with random data while storing their references and later making use of them, it proved faster on my system). Last time I checked it'd use actual underlying arrays when possible. Perhaps 1m is too big for some of those optimizations? Perhaps using sparse arrays is actually faster than allocating 1m-indexes arrays? I don't know.

    I guess what I meant to say is that, semantically, it is a hint for optimization. But you are in the hands of the current optimizer.

    I'd certainly enjoy reading some actual info on the optimization techniques being used in these cases on the current js engines.

  • Custom User Avatar

    http://jsperf.com/array-preallocated-vs-pushed/3

    There isn't much of a difference between pre-allocated and unallocated if you are using push() to grow the array. Preallocation does significantly improve performance if you use direct assignment to grow the array e.g. myArry[1] == x;.

    Edit: Changed the old jsperf that I found to have larger arrays to ensure any sort of internal buffer built by the JIT is exceeded.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution