Use the JavaScript version of the loop, and call that just once from C++.

If it's convenient from a packaging point of view, you can put it into a
string literal in your C++ source, and compile/run it from there.

Calls between C++ and JavaScript (in both directions) are always going to
have quite a bit of overhead; the best way to speed them up is to avoid
doing too many of them.


On Sat, May 16, 2020 at 10:36 PM Feng Wang <[email protected]> wrote:

> Thanks Yang. But I have to define the processing algorithm in javascript.
>
>
>
> Feng Wang
> 邮箱:[email protected]
>
> <https://maas.mail.163.com/dashi-web-extend/html/proSignature.html?ftlId=1&name=Feng+Wang&uid=jfwf2004%40gmail.com&iconUrl=https%3A%2F%2Fmail-online.nosdn.127.net%2Fqiyelogo%2FdefaultAvatar.png&items=%5B%22%E9%82%AE%E7%AE%B1%EF%BC%9Ajfwf2004%40gmail.com%22%5D>
>
> 签名由 网易邮箱大师 <https://mail.163.com/dashi/dlpro.html?from=mail88> 定制
>
> On 05/16/2020 22:03, Yang Guo <[email protected]> wrote:
> Put everything into an ArrayBuffer and call C++ just once.
>
> On Sat, May 16, 2020, 15:07 Feng Wang <[email protected]> wrote:
>
>> I have an image (1024x1024x3) that needs to be process pixel-by-pixel.
>> The per-pixel processing code is written using javascript. I've found that
>> it takes 277ms to loop through this handler in c++ code, and only 10ms to
>> loop in javascript code. I want to know how to make the c++ call javascript
>> function as fast as possible.
>>
>> //javascript per-pixel processing function
>> var pixelfunction=function( rgb , index )
>> {
>>   var outval = (rgb[0]-rgb[1]/(rgb[0]+rgb[1]) ;
>>   return outval;
>> }
>>
>>
>>
>> //c++ codes loop call pixelfunction
>> ...
>> v8::Function* pixelFunc = ...; // this Function pointer is pixelfunction
>> in javascript.
>> v8::Local<v8::Uint8Array> vals = ...; //
>> v8::Local<Value> targs[2] ;
>> for(int it = 0 ;it<1024*1024;++it)
>> {
>>   vals = ...; // get r,g,b values from image ;
>>   targs[0] = vals;
>>   targs[1] = v8::Integer::New( global , it );
>>   pixelFunc->Call( context , context->Global() , 2 , targs);//For
>> convenience, I'm giving up the return value.
>> }
>> ...
>>
>> -
>> //javascript loop call:
>> var vals=new Uint8Array(3);
>> for(var it = 0 ; it < 1024*1024 ; ++it )
>> {
>>   vals=...; //get r,g,b values from image
>>   pixelfunction( vals , it );//For convenience, I'm giving up the return
>> value.
>> }
>>
>> --
>> --
>> 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].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/v8-dev/dd3c7a28-1144-4e5f-a982-15e8558d7e5c%40googlegroups.com
>> <https://groups.google.com/d/msgid/v8-dev/dd3c7a28-1144-4e5f-a982-15e8558d7e5c%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> --
> 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].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/v8-dev/CAFSTc_g3hQ9P3bevpXqjux%2BZa%3DMcZmQRyjQ-uD62Mi3w_tkezg%40mail.gmail.com
> <https://groups.google.com/d/msgid/v8-dev/CAFSTc_g3hQ9P3bevpXqjux%2BZa%3DMcZmQRyjQ-uD62Mi3w_tkezg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> --
> --
> 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].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/v8-dev/1589661290983.hsmxtszv0tk5ohvnejt3hcts%40android.mail.163.com
> <https://groups.google.com/d/msgid/v8-dev/1589661290983.hsmxtszv0tk5ohvnejt3hcts%40android.mail.163.com?utm_medium=email&utm_source=footer>
> .
>

-- 
-- 
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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-dev/CAKSzg3QOYeOcxrxLWvSXdrhhvcFD1%3DYn%2BxubmM%2BivcaUdzQ3-A%40mail.gmail.com.

Reply via email to