📜 ⬆️ ⬇️

ObjectScript is a new programming language, faster than PHP and JS

ObjectScript is a new embedded and very lightweight object-oriented open source programming language. ObjectScript extends the capabilities of languages ​​such as JavaScript, Lua and PHP. The syntax is mainly taken from JavaScript, the multiple assignment is from Lua, the work with properties is from PHP.

ObjectScript 0.97-vm2 is faster than PHP 5.3.3 and JS by 34% and 61%, respectively.

As tested


Fannkuch algorithm was taken for testing . Quite a convenient test, one function with a parameter, when the parameter is increased by 1, the number of calculations increases about 10 times. Accordingly, the less time spent on the test, the better.
')
Fannkuch was implemented in ObjectScript, the full source:

print arg var fannkuch = function(n) { var p, q, s, sign, maxflips, sum = [], [], [], 1, 0, 0 var i for(i=1; i<=n; i++) p[i], q[i], s[i] = i, i, i for(;;){ // Copy and flip. var q1 = p[1] // Cache 1st element. if(q1 != 1){ for(i=2; i<=n; i++) q[i] = p[i] // Work on a copy. var flips = 1 for(;;){ var qq = q[q1] if(qq == 1){ // ... until 1st element is 1. sum = sum + sign*flips if(flips > maxflips){ maxflips = flips } // New maximum? break } q[q1] = q1 if(q1 >= 4){ var i, j = 2, q1 - 1 for(;;){ q[i], q[j] = q[j], q[i]; if(++i >= --j) break } } q1 = qq; flips++ } } // Permute. if(sign == 1){ p[2], p[1] = p[1], p[2] sign = -1 // Rotate 1<-2. }else{ p[2], p[3] = p[3], p[2] sign = 1 // Rotate 1<-2 and 1<-2<-3. for(i = 3;; i++){ // print "mark 4" var sx = s[i] if(sx != 1){ s[i] = sx-1 break } if(i == n) return sum, maxflips; // Out of permutations. s[i] = i // Rotate 1<-...<-i+1. var t = p[1] for(var j = 1; j <= i; j++){ p[j] = p[j+1] } p[i+1] = t } } } } var n = numberof(arg && arg[1]) || 5 var start_time = getTimeSec() var sum, flips = fannkuch(n) echo( sum"\n" "Pfannkuchen("n") = "flips"\n" "time = ", (getTimeSec() - start_time)"\n" ) 

In PHP, the full source:

 <?php function fannkuch($n) { $p = array(); $q = array(); $s = array(); $sign = 1; $maxflips = $sum = 0; for($i=1; $i<=$n; $i++) $p[$i] = $q[$i] = $s[$i] = $i; for(;;){ // Copy and flip. $q1 = $p[1]; // Cache 1st element. if($q1 != 1){ for($i=2; $i<=$n; $i++) $q[$i] = $p[$i]; // Work on a copy. $flips = 1; for(;;){ $qq = $q[$q1]; if($qq == 1){ // ... until 1st element is 1. $sum += $sign*$flips; if($flips > $maxflips){ $maxflips = $flips; } // New maximum? break; } $q[$q1] = $q1; if($q1 >= 4){ $i = 2; $j = $q1 - 1; for(;;){ $tmp = $q[$i]; $q[$i] = $q[$j]; $q[$j] = $tmp; if(++$i >= --$j) break; } } $q1 = $qq; $flips++; } } // Permute. if($sign == 1){ $tmp = $p[2]; $p[2] = $p[1]; $p[1] = $tmp; $sign = -1; // Rotate 1<-2. }else{ $tmp = $p[2]; $p[2] = $p[3]; $p[3] = $tmp; $sign = 1; // Rotate 1<-2 and 1<-2<-3. for($i = 3;; $i++){ $sx = $s[$i]; if($sx != 1){ $s[$i] = $sx-1; break; } if($i == $n) return array($sum, $maxflips); // Out of permutations. $s[$i] = $i; // Rotate 1<-...<-i+1. $t = $p[1]; for($j = 1; $j <= $i; $j++){ $p[$j] = $p[$j+1]; } $p[$i+1] = $t; } } } } function getTimeSec(){ list($usec, $sec) = explode(" ",microtime()); return ($usec + $sec); } $n = isset($argv[1]) ? $argv[1] : 5; echo "n: $n\n"; $start_time = getTimeSec(); $r = fannkuch($n); $sum = $r[0]; $flips = $r[1]; echo("$sum\nPfannkuchen($n) = $flips\n" . "time = ".(getTimeSec() - $start_time)."\n"); 

In JavaScript, the full source:

 var fannkuch = function(n) { var p = [], q = [], s = [], sign = 1, maxflips = 0, sum = 0; var i; for(i=1; i<=n; i++) p[i] = q[i] = s[i] = i; for(;;){ // Copy and flip. var q1 = p[1]; // Cache 1st element. if(q1 != 1){ for(i=2; i<=n; i++) q[i] = p[i]; // Work on a copy. var flips = 1; for(;;){ var qq = q[q1]; if(qq == 1){ // ... until 1st element is 1. sum = sum + sign*flips; if(flips > maxflips){ maxflips = flips; } // New maximum? break; } q[q1] = q1; if(q1 >= 4){ var i = 2, j = q1 - 1 for(;;){ var tmp = q[i]; q[i] = q[j]; q[j] = tmp; if(++i >= --j) break; } } q1 = qq; flips++; } } // Permute. if(sign == 1){ var tmp = p[2]; p[2] = p[1]; p[1] = tmp; sign = -1; // Rotate 1<-2. }else{ var tmp = p[2]; p[2] = p[3]; p[3] = tmp; sign = 1; // Rotate 1<-2 and 1<-2<-3. for(i = 3;; i++){ // print "mark 4" var sx = s[i]; if(sx != 1){ s[i] = sx-1; break; } if(i == n) return [sum, maxflips]; // Out of permutations. s[i] = i; // Rotate 1<-...<-i+1. var t = p[1]; for(var j = 1; j <= i; j++){ p[j] = p[j+1]; } p[i+1] = t; } } } } function getTimeSec(){ var d = new Date(); return (d.getTime() + d.getMilliseconds() / 1000.0) / 1000.0; } var n = 10; var start_time = getTimeSec(); var r = fannkuch(n); var sum = r[0], flips = r[1]; WScript.Echo( sum,"\n", "Pfannkuchen(",n,") = ",flips,"\n", "time = ",(getTimeSec() - start_time),"\n" ) 

Testing was conducted on the platform: Windows 7, CPU Core i7 2630QM 2Ghz. Fannkuch started with parameter 10 .

ObjectScript 0.97-vm2 is a release assembly with a numeric type of double (with float, it turns out to be about 10% faster, but I don’t give these numbers in the article, because PHP and JS are numeric, double, PHP also has integer type):

 c:\Sources\OS\bin\os.exe test_fannkuch.os 10 

output (time is average over 10 iterations):

 73196 Pfannkuchen(10) = 38 time = 20.0991 

PHP 5.3.3:

 c:\WebServers\usr\bin\php5.exe test_fannkuch.php 10 

output (time is average over 10 iterations):

 73196 Pfannkuchen(10) = 38 time = 26.853 

JS (parameter 10 is inside test_fannkuch.js):

 Cscript.exe test_fannkuch.js 

output (time is average over 10 iterations):

 73196 Pfannkuchen( 10 ) = 38 time = 32.3313 

Summary


The less time the better.

ObjectScript - 20.099 sec
PHP - 26.853 sec
JS - 32.331 sec

Take the standard time 20.099 and reduce the results to percentages:


You can download the ObjectScript sources and an example from this article at this link , open proj.win32 \ examples.sln , project profile_benchmark .

Characteristics of ObjectScript


OOP - there is
Functions as first class values ​​- is
Closings - is
Garbage Collector - Tri-Color Incremental Garbage collection
Bytecode compilation - is
Download the compiled program - there is
Modularity - is
Modules loaded in runtime
Cross-platform - there is
Integration with C / C ++ - is
Sources - open source
License - MIT (can be used in any products for free)
Ideas for the language borrowed from JavaScript, Lua and PHP
Goals and objectives of the language: scripting game and program logic, cross portability, web and server development
by Yevgeny Golovin (developer of oxsar.ru , etc.)
ObjectScript development time - 2 months

ObjectScript Development and Promotion


The potential for further optimization in speed has not yet been exhausted, but the current characteristics do not look pretty bad. Apparently with further optimization, you can slow down a bit. I plan to study the binding of different languages ​​and make a really convenient binding C ++ to the OS.

With great pleasure I will get you a job and wrap the API you need in OS :)

PS the project requires investment, on cooperation and just questions, you can safely write to evgeniy.golovin [AT] unitpoint.ru

Other relevant ObjectScript articles:

Source: https://habr.com/ru/post/153455/


All Articles