compared to the JS function, the C implementation has a lot of overhead to copy the data back and forth, so it's not surprising that this overhead shows up especially on larger arrays
On Mon, Oct 24, 2016 at 2:47 AM Samantha Krieger <[email protected]> wrote: > Just wondering if I'm crazy/doing something incredibly stupid. > > I wrote this sort function using v8 (wrapper around qsort): > > void Sort(const FunctionCallbackInfo<Value>& args) { > Isolate* isolate = Isolate::GetCurrent(); > HandleScope scope(isolate); > > if (args.Length() < 1 || !args[0]->IsArray()) { > isolate->ThrowException( > Exception::TypeError( > String::NewFromUtf8(isolate, > "First argument should be an array"))); > return; > } > > Handle<Array> arr = Handle<Array>::Cast(args[0]); > int size = arr->Length(); > double other_arr[size]; > for (int i = 0; i < size; i++){ > other_arr[i] = arr->Get(i)->NumberValue(); > } > > > qsort(other_arr, size, sizeof(other_arr[0]), compare); > Handle<Array> res = Array::New(isolate, size); > for (int i = 0; i < size; ++i) { > // printf ("I val: %f\n",other_arr[i]); > res->Set(i, Number::New(isolate, other_arr[i])); > } > > args.GetReturnValue().Set(res); > > } > > > > when i run it in js from node, its markedly slower than the equivalent js > sort (which I understand v8 [].sort() uses qsort under the hood) for objects > between 1000 - 10000+ > > > var sort = require('bindings')('sort.node'); > > > function benchmarkSort(size){ > > var arr = helpers.generateArray(size); > var cres = benchmark(sort.sort, [arr], 10); > > > var arr = helpers.generateArray(size); > var jsres = benchmark(helpers.sort, [arr], 10); > return { > c: cres, > js: jsres > }; > > } > > > function benchmark(fn, args, times){ > var noTimes = (typeof times != 'undefined'? times: NO_TIMES); > var start = Date.now(); > var result = run(fn, noTimes, args); > var duration = Date.now() - start; > return duration; > } > > > ----- Cost for sorting 100 ------- > JS time: 1 msec > C time: 1 msec > ----- Cost for sorting 1000 ------- > JS time: 4 msec > C time: 3 msec > ----- Cost for sorting 10000 ------- > JS time: 22 msec > C time: 37 msec > ----- Cost for sorting 100000 ------- > JS time: 278 msec > C time: 441 msec > > > These results seem incredibly strange. Am I doing something incredibly > stupid/missing something? Can someone explain whats going on? Much > appreciated!!!! > > -- > -- > v8-dev mailing list > [email protected] > http://groups.google.com/group/v8-dev > --- > You received this message because you are subscribed to the Google Groups > "v8-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- -- v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev --- You received this message because you are subscribed to the Google Groups "v8-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
