Calc
calc
is an asynchronous function that we want to do profiling (profiling). By convention, its last argument is one callback
. We use it like this calc
:
Calc (ARG, (err, res) => Console.log (Err | | res))
Perhaps the simplest calc
way to dissect performance on such a function is to add a timing logic to where we need to analyze:
Const T0 = date.now ()
Calc (ARG, (err, res) => {
Const T1 = date.now ()
console.log (' log:time: ${t1 = t0} ') C10/>console.log (Err | | res)
})
However, this is not a reusable solution. Every time we want to time a function, we have to introduce a t0 in the outer scope and change it callback
to measure and record the timing.
The ideal way for me is to be able to clock it simply by wrapping an asynchronous function:
Timeit (Calc) (Arg, (err, res) => Console.log (Err | | res))
timeIt
Needs to be able to perform profiling and record execution times well for each asynchronous function.
Note that timeIt(calc)
there is the same function signature as the original Calc function, which accepts the same parameter and returns the same value, and it simply adds an attribute to the Cale (the feature that can be logged).
Calc and Timeit (Calc) can be substituted for each other at any time.
timeIt
itself is a higher order function because it takes a function and returns a function. In our case, it accepts the Calc asynchronous function and returns a function that has the same parameters and return value as Calc.
Here's how we implement the Timeit function:
Const TIMEIT = R.curry ((f) => (... args) => {
const T0 = date.now ()
Const Nargs = r.init (args)
Co NST callback = R.last (args)
Nargs.push ((... args) => {
Const T1 = Date.now () callback
(... args)
T1-t0 (args)
})
f (... nargs)
})
const TIMEIT1 = Timeit (
(t, err, RES) => Console.log (' log: ${err | | res} produced after: ${t} ')
Const CALC = (x, y, Z, callback) =>
settimeout ( () => callback (NULL, X * y/z), 1000)
Calc (7, 3, (err, res) => Console.log (Err | | res))
timeIt1 (Calc) (7, 3, (err, res) => Console.log (Err | | res))
This timeIt
implementation accepts two parameters:
A function is used to generate profiling results
F: We want to do an analytic asynchronous function
timeIt1
is a convenient and practical function, it only uses the console.log
recording time to measure the result. We define it by passing parameters to a more general timeIt
function report
.
We've achieved the goal, and now we can just wrap the asynchronous function in and timeIt1
then we can clock it:
TimeIt1 (Calc) (7, 3, (err, res) => Console.log (Err | | res))
A common timeIt
function receives a report
callback function and an asynchronous function and returns a new asynchronous function with the same parameter and return value as the original function. We can use this:
Timeit (Time
, ... result) =>//
The Callback:log of the time, Asyncfunc
) (
parameters ...,
=>//result of the async function
)
Now let's go deep into it timeIt
. We can simply generate a generic function similar to the one timeIt1
because timeIt
R.curry
of the use of the section.
I'm not going to discuss the subject in this article, but the following code illustrates the main uses of division:
Const F = R.curry ((x, y) => x + y)
f (1)//= one
f (1) (a)/= = one
const PLUS1 = f (1)
Plus1 (10)// = = 11
On the other hand, there are several problems with the implementation of this approach: Timeit
(... args) => {
Const T1 = date.now ()
callback (... args) (
t1-t0, ... args)
}
This is an anonymous function (also known as Lambda,callback), which is invoked after the original function is executed asynchronously. The main problem is that this function has no mechanism to handle exceptions. If an callback
exception is thrown, report
it will never be invoked.
We can add one try / catch
to this lambda
function, but the root of the problem is the callback
report
two void
functions that are not associated with each other. timeIt
contains two continuations (continuations) ( report
and callback
). If we just console
record the execution time or if we are certain report
that no exceptions are callback
thrown, then everything is fine. But if we want to perform some behavior based on the profiling results (called automatic expansion) then we need to strengthen and clarify the continuation sequence in our program.
Well, the above article of the entire content, hope for everyone's study and work to help, if there is doubt can message exchange.