Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-13-2010, 03:03 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
GBench

Benchmarking!
If you have ever found yourself trying to make a script run faster, it can be a pain. GBench (a benchmarker) makes it less of a pain:
  1. You create a function you want to benchmark
  2. You join("utility_benchmark");
  3. You call this.defaultMain({this.bench("someName", temp.yourFunctionHere)});

It will run 100 samples (basically a sample is enough of your function calls to take up at least 0.01 secs).

Then it will give you a histogram that shows each sample and how long it took.

And then it will give you something called a "Kernel Density Estimation". That is a statistics term for a pretty simple concept:
In our case, look at any point on the kernel density estimate, and that is your probability of your function taking that long.



Example!
PHP Code:
this.join("utility_benchmark");
//#CLIENTSIDE

// some example function you wrote
function fib(temp.n) {
  if (
temp.== 0) {
    return 
0;
  }
  if (
temp.== 1) {
    return 
1;
  }
  return 
fib(temp.n-1) + fib(temp.n-2);
}

// just in case
function onCreated() {
  
this.maxlooplimit 20000;
}


function 
onWeaponFired() {
  
// this is what we will be testing
  
temp.= function () {
    return 
this.fib(18);
  };

  
// let's go!
  
this.defaultMain({
    
this.bench("fib"temp.f)
  });

In your F2, you will get:
NPC Code:
benchmarking fib 
collecting 100 samples, 1 iterations each, in estimated 4.057331309 s
mean: 41.656855829 ms, lb: 41.109135379 ms, ub: 42.937021324 ms



And, then you will get your histogram and kernel density estimate (all times are in milliseconds):


The big hill on the kernel density estimate shows that most likely the function will take around 41.6 ms to run. The smaller hill near the end means there is a slight chance that the function will take 43 ms to run.



Known Bugs
  • On some computers, in v5, some samples take a negative amount of time to run. I have no clue why this would happen, since it just compares timevar2s.
  • In v6, division doesn't work nicely and can make the graph axis a bit hard to read.



The magic is attached!
Attached Thumbnails
Click image for larger version

Name:	benchmarks.gif
Views:	746
Size:	22.5 KB
ID:	50554  
Attached Files
File Type: txt algorithm_quicksort.txt (1.9 KB, 337 views)
File Type: txt utility_benchmark.txt (2.6 KB, 339 views)
File Type: txt utility_graph.txt (4.3 KB, 371 views)
File Type: txt utility_statistics.txt (4.4 KB, 340 views)

Last edited by WhiteDragon; 03-13-2010 at 03:16 AM..
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:47 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.