add statistical information

This commit is contained in:
Valk Richard Li 2021-06-19 00:32:10 +08:00
parent 0e979a6e7b
commit cdf7b92a8e
5 changed files with 109 additions and 59 deletions

View File

@ -243,6 +243,55 @@ f(1024,2048);
0x00000011: nop 0x00000000
```
## Test data
### version 6.5 gc(i5-8250U windows10)
running time and gc time:
|file|call|total time|gc time|
|:----|:----|:----|:----|
|pi.nas|12000049|0.593s|0.222s|
|fib.nas|10573747|2.838s|0.187s|
|bp.nas|4419829|1.99s|0.18s|
|bigloop.nas|4000000|0.419s|0.039s|
|mandelbrot.nas|1044630|0.433s|0.041s|
|life.nas|817112|8.557s|0.199s|
|ascii-art.nas|45612|0.48s|0.027s|
|calc.nas|8089|0.068s|0.006s|
|quick_sort.nas|2768|0.107s|0s|
|bfs.nas|2471|1.763s|0.003s|
operands calling frequency:
|file|1st called op|2nd called op|3rd called op|4th called op|5th called op|
|:----|:----|:----|:----|:----|:----|
|pi.nas|callg|pop|mcallg|pnum|pone|
|fib.nas|calll|pnum|callg|less|jf|
|bp.nas|calll|callg|pop|callv|addeq|
|bigloop.nas|pnum|less|jf|callg|pone|
|mandelbrot.nas|callg|mult|loadg|pnum|pop|
|life.nas|calll|callv|pnum|jf|callg|
|ascii-art.nas|calll|pop|mcalll|callg|callb|
|calc.nas|calll|pop|pstr|mcalll|jmp|
|quick_sort.nas|calll|pop|jt|jf|less|
|bfs.nas|calll|pop|callv|mcalll|jf|
operands calling total times:
|file|1st called time|2nd called time|3rd called time|4th called time|5th called time|
|:----|:----|:----|:----|:----|:----|
|pi.nas|6000004|6000003|6000000|4000005|4000002|
|fib.nas|17622792|10573704|7049218|7049155|7049155|
|bp.nas|7081480|4227268|2764676|2617112|2065441|
|bigloop.nas|4000001|4000001|4000001|4000001|4000000|
|mandelbrot.nas|1519632|563856|290641|286795|284844|
|life.nas|2114371|974244|536413|534794|489743|
|ascii-art.nas|37906|22736|22402|18315|18292|
|calc.nas|191|124|109|99|87|
|quick_sort.nas|16226|5561|4144|3524|2833|
|bfs.nas|24707|16297|14606|14269|8672|
## How to Use Nasal to Program
### basic value type
@ -496,17 +545,26 @@ struct FUNC_TABLE
nasal_val* (*func)(std::vector<nasal_val*>&,nasal_gc&);
} builtin_func[]=
{
{"__builtin_std_cout",builtin_print},
{"", NULL }
{"__builtin_print",builtin_print},
{nullptr, nullptr }
};
```
At last,warp the '__builtin_std_cout' in a nasal file:
At last,warp the '__builtin_print' in a nasal file:
```javascript
var print=func(elements...)
var print=func(elems...)
{
return __builtin_std_cout(elements);
return __builtin_print(elems);
};
```
In fact the arguments that '__builtin_print' uses is not necessary,So writting it like this is also right:
```javascript
var print=func(elems...)
{
return __builtin_print;
};
```
@ -525,7 +583,7 @@ The value got before will be collected,but stil in use in this builtin function,
So use builtin_alloc in builtin functions like this:
```C++
nasal_val* builtin_getkeys(std::vector<nasal_val*>& local_scope,nasal_gc& gc)
nasal_val* builtin_keys(std::vector<nasal_val*>& local_scope,nasal_gc& gc)
{
nasal_val* hash_addr=local_scope[1];
if(hash_addr->type!=vm_hash)

17
lib.nas
View File

@ -4,20 +4,21 @@ var import=func(filename)
}
var print=func(elems...)
{
return __builtin_std_cout(elems);
return __builtin_print(elems);
};
var println=func(elems...)
{
__builtin_std_cout(elems);
return print('\n');
__builtin_print(elems);
elems=['\n'];
return __builtin_print(elems);
}
var append=func(vec,elems...)
{
return __builtin_push_back(vec,elems);
return __builtin_append(vec,elems);
}
var setsize=func(vec,size)
{
return __builtin_set_size(vec,size);
return __builtin_setsize(vec,size);
}
var system=func(str)
{
@ -41,7 +42,7 @@ var rand=func(seed=nil)
}
var id=func(object)
{
return __builtin_get_id(object);
return __builtin_id(object);
}
var int=func(val)
{
@ -53,7 +54,7 @@ var num=func(val)
}
var pop=func(vec)
{
return __builtin_pop_back(vec);
return __builtin_pop(vec);
}
var str=func(num)
{
@ -73,7 +74,7 @@ var delete=func(hash,key)
}
var keys=func(hash)
{
return __builtin_get_keys(hash);
return __builtin_keys(hash);
}
var time=func(begin_time)
{

View File

@ -44,7 +44,7 @@ nasal_val* builtin_atan2(std::vector<nasal_val*>&,nasal_gc&);
nasal_val* builtin_time(std::vector<nasal_val*>&,nasal_gc&);
nasal_val* builtin_contains(std::vector<nasal_val*>&,nasal_gc&);
nasal_val* builtin_delete(std::vector<nasal_val*>&,nasal_gc&);
nasal_val* builtin_getkeys(std::vector<nasal_val*>&,nasal_gc&);
nasal_val* builtin_keys(std::vector<nasal_val*>&,nasal_gc&);
nasal_val* builtin_import(std::vector<nasal_val*>&,nasal_gc&);
nasal_val* builtin_die(std::vector<nasal_val*>&,nasal_gc&);
nasal_val* builtin_type(std::vector<nasal_val*>&,nasal_gc&);
@ -69,9 +69,9 @@ struct FUNC_TABLE
nasal_val* (*func)(std::vector<nasal_val*>&,nasal_gc&);
} builtin_func[]=
{
{"__builtin_std_cout", builtin_print },
{"__builtin_push_back",builtin_append },
{"__builtin_set_size", builtin_setsize },
{"__builtin_print", builtin_print },
{"__builtin_append", builtin_append },
{"__builtin_setsize", builtin_setsize },
{"__builtin_system", builtin_system },
{"__builtin_input", builtin_input },
{"__builtin_sleep", builtin_sleep },
@ -79,10 +79,10 @@ struct FUNC_TABLE
{"__builtin_fout", builtin_fout },
{"__builtin_split", builtin_split },
{"__builtin_rand", builtin_rand },
{"__builtin_get_id", builtin_id },
{"__builtin_id", builtin_id },
{"__builtin_int", builtin_int },
{"__builtin_num", builtin_num },
{"__builtin_pop_back", builtin_pop },
{"__builtin_pop", builtin_pop },
{"__builtin_str", builtin_str },
{"__builtin_size", builtin_size },
{"__builtin_xor", builtin_xor },
@ -100,7 +100,7 @@ struct FUNC_TABLE
{"__builtin_time", builtin_time },
{"__builtin_contains", builtin_contains},
{"__builtin_delete", builtin_delete },
{"__builtin_get_keys", builtin_getkeys },
{"__builtin_keys", builtin_keys },
{"__builtin_import", builtin_import },
{"__builtin_die", builtin_die },
{"__builtin_type", builtin_type },
@ -110,7 +110,7 @@ struct FUNC_TABLE
{"__builtin_right", builtin_right },
{"__builtin_cmp", builtin_cmp },
{"__builtin_chr", builtin_chr },
{"", nullptr }
{nullptr, nullptr }
};
nasal_val* builtin_print(std::vector<nasal_val*>& local_scope,nasal_gc& gc)
@ -648,7 +648,7 @@ nasal_val* builtin_delete(std::vector<nasal_val*>& local_scope,nasal_gc& gc)
hash_addr->ptr.hash->elems.erase(*key_addr->ptr.str);
return gc.nil_addr;
}
nasal_val* builtin_getkeys(std::vector<nasal_val*>& local_scope,nasal_gc& gc)
nasal_val* builtin_keys(std::vector<nasal_val*>& local_scope,nasal_gc& gc)
{
nasal_val* hash_addr=local_scope[1];
if(hash_addr->type!=vm_hash)

View File

@ -273,8 +273,7 @@ void nasal_codegen::add_sym(std::string& name)
int nasal_codegen::local_find(std::string& name)
{
int index=-1;
int cnt=0;
int index=-1,cnt=0;
for(auto& i:local)
{
for(int j=0;j<i.size();++j)
@ -293,10 +292,9 @@ int nasal_codegen::global_find(std::string& name)
return -1;
}
void nasal_codegen::gen(unsigned char op,unsigned int index)
void nasal_codegen::gen(uint8_t op,uint32_t num)
{
opcode opr(op,index);
exec_code.push_back(opr);
exec_code.push_back({op,num});
return;
}
@ -422,7 +420,7 @@ void nasal_codegen::call_gen(nasal_ast& ast)
switch(tmp.get_type())
{
case ast_callh:call_hash(tmp);break;
case ast_callv: call_vec(tmp); break;
case ast_callv:call_vec(tmp); break;
case ast_callf:call_func(tmp);break;
}
}
@ -432,10 +430,12 @@ void nasal_codegen::call_gen(nasal_ast& ast)
void nasal_codegen::call_id(nasal_ast& ast)
{
std::string& str=ast.get_str();
for(int i=0;builtin_func[i].func;++i)
for(int i=0;builtin_func[i].name;++i)
if(builtin_func[i].name==str)
{
gen(op_callb,i);
if(local.empty())
die("builtin functions work in a local scope.",ast.get_line());
return;
}
int index=local_find(str);
@ -528,6 +528,12 @@ void nasal_codegen::mcall(nasal_ast& ast)
void nasal_codegen::mcall_id(nasal_ast& ast)
{
std::string& str=ast.get_str();
for(int i=0;builtin_func[i].name;++i)
if(builtin_func[i].name==str)
{
die("cannot change builtin function.",ast.get_line());
return;
}
int index=local_find(str);
if(index>=0)
{
@ -564,10 +570,7 @@ void nasal_codegen::single_def(nasal_ast& ast)
std::string& str=ast.get_children()[0].get_str();
add_sym(str);
calc_gen(ast.get_children()[1]);
if(local.empty())
gen(op_loadg,global_find(str));
else
gen(op_loadl,local_find(str));
local.empty()?gen(op_loadg,global_find(str)):gen(op_loadl,local_find(str));
return;
}
void nasal_codegen::multi_def(nasal_ast& ast)
@ -582,10 +585,7 @@ void nasal_codegen::multi_def(nasal_ast& ast)
calc_gen(vals[i]);
std::string& str=ids[i].get_str();
add_sym(str);
if(local.empty())
gen(op_loadg,global_find(str));
else
gen(op_loadl,local_find(str));
local.empty()?gen(op_loadg,global_find(str)):gen(op_loadl,local_find(str));
}
}
else
@ -596,10 +596,7 @@ void nasal_codegen::multi_def(nasal_ast& ast)
gen(op_callvi,i);
std::string& str=ids[i].get_str();
add_sym(str);
if(local.empty())
gen(op_loadg,global_find(str));
else
gen(op_loadl,local_find(str));
local.empty()?gen(op_loadg,global_find(str)):gen(op_loadl,local_find(str));
}
gen(op_pop,0);
}
@ -714,7 +711,6 @@ void nasal_codegen::while_gen(nasal_ast& ast)
void nasal_codegen::for_gen(nasal_ast& ast)
{
opcode op;
switch(ast.get_children()[0].get_type())
{
case ast_null:break;
@ -788,10 +784,7 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
{
std::string& str=ast.get_children()[0].get_children()[0].get_str();
add_sym(str);
if(local.empty())
gen(op_loadg,global_find(str));
else
gen(op_loadl,local_find(str));
local.empty()?gen(op_loadg,global_find(str)):gen(op_loadl,local_find(str));
}
else
{
@ -818,10 +811,7 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
{
std::string& str=ast.get_children()[0].get_children()[0].get_str();
add_sym(str);
if(local.empty())
gen(op_loadg,global_find(str));
else
gen(op_loadl,local_find(str));
local.empty()?gen(op_loadg,global_find(str)):gen(op_loadl,local_find(str));
}
else
{

View File

@ -4,20 +4,21 @@ var import=func(filename)
}
var print=func(elems...)
{
return __builtin_std_cout(elems);
return __builtin_print(elems);
};
var println=func(elems...)
{
__builtin_std_cout(elems);
return print('\n');
__builtin_print(elems);
elems=['\n'];
return __builtin_print(elems);
}
var append=func(vec,elems...)
{
return __builtin_push_back(vec,elems);
return __builtin_append(vec,elems);
}
var setsize=func(vec,size)
{
return __builtin_set_size(vec,size);
return __builtin_setsize(vec,size);
}
var system=func(str)
{
@ -41,7 +42,7 @@ var rand=func(seed=nil)
}
var id=func(object)
{
return __builtin_get_id(object);
return __builtin_id(object);
}
var int=func(val)
{
@ -53,7 +54,7 @@ var num=func(val)
}
var pop=func(vec)
{
return __builtin_pop_back(vec);
return __builtin_pop(vec);
}
var str=func(num)
{
@ -73,7 +74,7 @@ var delete=func(hash,key)
}
var keys=func(hash)
{
return __builtin_get_keys(hash);
return __builtin_keys(hash);
}
var time=func(begin_time)
{