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

By , 9 Oct 2012

ObjectScript is a new embedded programing language that mixes the benefits of JavaScript, Lua, and PHP. ObjectScript has syntax from JavaScript, multiple results from Lua, OOP from PHP, and much more.

ObjectScript 0.97-vm2 is 34% faster than PHP 5.3.3, and 61% faster than JavaScript.

## Testing

I've used Fannkuch test algorithm. It is quite an easy to test, one function with a parameter. The number of calculations increases approximately by 10 times if you increase the parameter by 1.

ObjectScript implementation of the fannkuch:

```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"
) ```

PHP implementation of the fannkuch:

```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");
```

JavaScript implementation of the fannkuch:

```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"
)
```

I've used the following testing platform: Windows7, CPU Core i7 2630QM 2Ghz. The test was performed with the fannkuch by passing 10 as parameter.

ObjectScript 0.97-vm2 release version with numerics of double type (it's a bit faster to use float but PHP and JS have numerics of double type).

```c:\Sources\OS\bin\os.exe test_fannkuch.os 10
```
The result of OS testing (the average time of 10 iterations is shown):
```73196
Pfannkuchen(10) = 38
time = 20.0991
```
PHP 5.3.3:
```c:\WebServers\usr\bin\php5.exe test_fannkuch.php 10
```
The result of PHP testing (the average time of 10 iterations is shown)
```73196
Pfannkuchen(10) = 38
time = 26.853
```
JS (parameter 10 is in the test_fannkuch.js):
```Cscript.exe test_fannkuch.js
```
The result of JS testing (the average time of 10 iterations is shown)
```73196
Pfannkuchen( 10 ) =  38
time =  32.3313
```

## Summary

• ObjectScript - 20.099 sec
• PHP - 26.853 sec
• JS - 32.331 sec
Note that the less time the better.

The result in percentages if we use 20.099 as standard time:
• OS is 34% faster than PHP
• OS is 61% faster than JS

P.S. Speed of node.js wasn't tested because of the node.js doesn't have a virtual machine (VM) and doesn't execute the bytecode (node.js uses V8 engine and JIT technology by Google). ObjectScript and PHP have their own VM and execute the bytecode, and they can be aimed to do the same tasks.

Nevertheless the test has been done.

## Characteristics of the language ObjectScript

• OOP - yes
• First-class functions - yes
• Closure - yes
• Garbage collector - Tri-Color Incremental Garbage collection
• Bytecode compilation - yes
• Load of the compiled program - yes
• Modules support - yes
• Load modules in the runtime - yes
• Cross-platform support - yes
• C++ integration - yes
• Source code - open source code
• License - MIT (can be used in any products free of charge)
• Development time - 2 months
• ObjectScript goals and objectives: game scripting, script program logic, cross-platform programing, mobile platforms, web and server development

You could view videos of mobile engine made with ObjectScript:

My name is Evgeniy Golovin, I am the author of ObjectScript.

My current task list: finalize specification of OS, create website of OS community, write technical documentation, examples, integrate MySql, PostgreSQL, mongodb, regular expressions library and some other libraries, make more tests, create IDE for ObjectScript development and debugging, create compiler OS to JS, implement JIT and much more.

It'd be nice to integrate OS with one of cross-platform 3D engine. Looking forward to your reply! You could skype me on egolovin. Other relevant articles about ObjectScript:

 unitpoint United States Member
No Biography provided

Votes of 3 or less require a comment

Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.