Tag Archive for execution

Python – Create a function in runtime

import types

def create_function(name, args):

def y(): pass

y_code = types.CodeType(args,
y.func_code.co_nlocals,
y.func_code.co_stacksize,
y.func_code.co_flags,
y.func_code.co_code,
y.func_code.co_consts,
y.func_code.co_names,
y.func_code.co_varnames,
y.func_code.co_filename,
name,
y.func_code.co_firstlineno,
y.func_code.co_lnotab)

return types.FunctionType(y_code, y.func_globals, name)

myfunc = create_function('myfunc', 3)

print repr(myfunc)
print myfunc.func_name
print myfunc.func_code.co_argcount

myfunc(1,2,3,4)
# TypeError: myfunc() takes exactly 3 arguments (4 given)

source

Calculating script execution time

<?php
$start = (float) array_sum(explode(' ',microtime()));

// put you code that wanted to render at here

$end = (float) array_sum(explode(' ',microtime()));

echo "Processing time: ". sprintf("%.4f", ($end-$start))." seconds";
?>

source

Closure Example Relating to ‘this’ Execution Context

$(function(){
//Let ID equal a string
this.id = 'someID';
//Set function outer to this
var outer = this;
$('*').each(function(){
/**
* Since outer is being pulled in from outside the function, it still
* retains the external context to this. Alert will be 'someID'
*/
alert(outer.id);
});
});

source