import time

76 views
Skip to first unread message

roberci...@gmail.com

unread,
Sep 13, 2016, 12:02:08 PM9/13/16
to RapydScript
Hi I need import time to measure speed of execution for loop in RapydScript

I have this code:


import time

#zwykla petla for
t0 = time.time()
for i in range(1000000):
    a = 1
print("petla for: ", time.time()-t0, "ms")

But I have error:

/home/home/RapydScript/lib/parse.js:1368
            throw "Failed Import: '" + key + "' module doesn't exist in any of the import directories: " + import_dirs.join(", ");
            ^
Failed Import: 'time' module doesn't exist in any of the import directories: ., /home/home/RapydScript/src/lib



So I change import time to:

from stdlib import time
And now i have:

(function(){"use strict";var ՐՏ_Temp;function ՐՏ_Iterable(iterable){if(Array.isArray(iterable)||iterable instanceof String||typeof iterable==="string"){return iterable}return Object.keys(iterable)}function ՐՏ_bind(fn,thisArg){var ret;if(fn.orig){fn=fn.orig}if(thisArg===false){return fn}ret=function(){return fn.apply(thisArg,arguments)};ret.orig=fn;return ret}function range(start,stop,step){var length,idx,range;if(arguments.length<=1){stop=start||0;start=0}step=arguments[2]||1;length=Math.max(Math.ceil((stop-start)/step),0);idx=0;range=new Array(length);while(idx<length){range[idx++]=start;start+=step}return range}function len(obj){var length;if(typeof obj.indexOf==="function"){return obj.length}length=0;for(var i in obj){length++}return length}function eq(a,b){var i;"\n    Equality comparison that works with all data types, returns true if structure and\n    contents of first object equal to those of second object\n\n    Arguments:\n        a: first object\n        b: second object\n    ";if(a===b){return true}if(Array.isArray(a)&&Array.isArray(b)||a instanceof Object&&b instanceof Object){if(a.constructor!==b.constructor||a.length!==b.length){return false}if(Array.isArray(a)){for(i=0;i<len(a);i++){if(!eq(a[i],b[i])){return false}}}else{if(Object.keys(a).length!==Object.keys(b).length){return false}var ՐՏ_Iter0=ՐՏ_Iterable(a);for(var ՐՏ_Index0=0;ՐՏ_Index0<ՐՏ_Iter0.length;ՐՏ_Index0++){i=ՐՏ_Iter0[ՐՏ_Index0];if(!eq(a[i],b[i])){return false}}}return true}return false}function ՐՏ_in(val,arr){if(typeof arr.indexOf==="function"){return arr.indexOf(val)!==-1}return arr.hasOwnProperty(val)}function dir(item){var arr;arr=[];for(var i in item){arr.push(i)}return arr}function ՐՏ_extends(child,parent){child.prototype=Object.create(parent.prototype);child.prototype.constructor=child}function ValueError(){ValueError.prototype.__init__.apply(this,arguments)}ՐՏ_extends(ValueError,Error);ValueError.prototype.__init__=function __init__(message){var self=this;self.name="ValueError";self.message=message};function ՐՏ_print(){if(typeof console==="object"){console.log.apply(console,arguments)}}var ՐՏ_modules = {};ՐՏ_modules["stdlib"] = {};(function(){var __name__="stdlib";var str;str=JSON.stringify;String.prototype.find=String.prototype.indexOf;String.prototype.strip=String.prototype.trim;String.prototype.lstrip=String.prototype.trimLeft;String.prototype.rstrip=String.prototype.trimRight;String.prototype.join=function(iterable){return iterable.join(this)};String.prototype.zfill=function(size){var s;s=this;while(s.length<size){s="0"+s}return s};function list(iterable){if(typeof iterable==="undefined")iterable=[];var result,i;result=[];var ՐՏ_Iter1=ՐՏ_Iterable(iterable);for(var ՐՏ_Index1=0;ՐՏ_Index1<ՐՏ_Iter1.length;ՐՏ_Index1++){i=ՐՏ_Iter1[ՐՏ_Index1];result.append(i)}return result}Array.prototype.append=Array.prototype.push;Array.prototype.find=Array.prototype.indexOf;Array.prototype.index=function(index){var val;val=this.find(index);if(val===-1){throw new ValueError(str(index)+" is not in list")}return val};Array.prototype.insert=function(index,item){this.splice(index,0,item)};Array.prototype.pop=function(index){if(typeof index==="undefined")index=this.length-1;return this.splice(index,1)[0]};Array.prototype.extend=function(array2){this.push.apply(this,array2)};Array.prototype.remove=function(item){var index;index=this.find(item);this.splice(index,1)};Array.prototype.copy=function(){return this.slice(0)};function dict(iterable){var result,key;result={};var ՐՏ_Iter2=ՐՏ_Iterable(iterable);for(var ՐՏ_Index2=0;ՐՏ_Index2<ՐՏ_Iter2.length;ՐՏ_Index2++){key=ՐՏ_Iter2[ՐՏ_Index2];result[key]=iterable[key]}return result}if(typeof Object.getOwnPropertyNames!=="function"){dict.keys=function(hash){var keys;keys=[];
        for (var x in hash) {
            if (hash.hasOwnProperty(x)) {
                keys.push(x);
            }
        }
        ;return keys}}else{dict.keys=function(hash){return Object.getOwnPropertyNames(hash)}}dict.values=function(hash){var vals,key;vals=[];var ՐՏ_Iter3=ՐՏ_Iterable(dict.keys(hash));for(var ՐՏ_Index3=0;ՐՏ_Index3<ՐՏ_Iter3.length;ՐՏ_Index3++){key=ՐՏ_Iter3[ՐՏ_Index3];vals.append(hash[key])}return vals};dict.items=function(hash){var items,key;items=[];var ՐՏ_Iter4=ՐՏ_Iterable(dict.keys(hash));for(var ՐՏ_Index4=0;ՐՏ_Index4<ՐՏ_Iter4.length;ՐՏ_Index4++){key=ՐՏ_Iter4[ՐՏ_Index4];items.append([key,hash[key]])}return items};dict.copy=dict;dict.clear=function(hash){var key;var ՐՏ_Iter5=ՐՏ_Iterable(dict.keys(hash));for(var ՐՏ_Index5=0;ՐՏ_Index5<ՐՏ_Iter5.length;ՐՏ_Index5++){key=ՐՏ_Iter5[ՐՏ_Index5];delete hash[key]}};ՐՏ_modules["stdlib"]["str"] = str;ՐՏ_modules["stdlib"]["list"] = list;ՐՏ_modules["stdlib"]["dict"] = dict})();(function(){var __name__ = "__main__";var t0,a,i;var time=ՐՏ_modules["stdlib"].time;t0=time.time();for(i=0;i<1e6;i++){a=1}ՐՏ_print("petla for: ",time.time()-t0,"ms")})();})();

what Can I do to run this?

Alexander Tsepkov

unread,
Sep 13, 2016, 12:15:19 PM9/13/16
to roberci...@gmail.com, RapydScript
RapydScript does not come with a time module out of the box, you can see which modules exist in the libs directory: https://github.com/atsepkov/RapydScript/tree/master/src/lib

Since RS is JavaScript-based, you have a lot of options for measuring time, however. If your function takes a long time to execute and minor inaccuracies don't matter, just use the Date module:

t0 = Date.now()
for i in range(1000000):
    ...
print("petla for: ", Date.now()-t0, "ms")

If the operation is relatively short, as the one you mention, you can use benchmark.js (https://benchmarkjs.com/) instead (RapydScript itself uses similar method for benchmarking its own performance):

bench = Benchmark.Suite

bench.add(def():
    for i in range(1000000):
        ...
).on("complete", def():
    print("petla for: ", this[0].stats.mean, "ms")
).run()

Alexander Tsepkov

unread,
Sep 13, 2016, 12:16:20 PM9/13/16
to roberci...@gmail.com, RapydScript
Just to clarify, the reason to use benchmark for short operations is because Date.now() will not be accurate enough.
Message has been deleted

roberci...@gmail.com

unread,
Sep 13, 2016, 3:06:58 PM9/13/16
to RapydScript, roberci...@gmail.com
Ok I follow your advice.
I have file index.py
In terminal I type
rapydscript index.py
And get this error:
 (function(){"use strict";var ՐՏ_Temp;function ՐՏ_Iterable(iterable){if(Array.isArray(iterable)||iterable instanceof String||typeof iterable==="string"){return iterable}return Object.keys(iterable)}function ՐՏ_bind(fn,thisArg){var ret;if(fn.orig){fn=fn.orig}if(thisArg===false){return fn}ret=function(){return fn.apply(thisArg,arguments)};ret.orig=fn;return ret}function range(start,stop,step){var length,idx,range;if(arguments.length<=1){stop=start||0;start=0}step=arguments[2]||1;length=Math.max(Math.ceil((stop-start)/step),0);idx=0;range=new Array(length);while(idx<length){range[idx++]=start;start+=step}return range}function len(obj){var length;if(typeof obj.indexOf==="function"){return obj.length}length=0;for(var i in obj){length++}return length}function eq(a,b){var i;"\n    Equality comparison that works with all data types, returns true if structure and\n    contents of first object equal to those of second object\n\n    Arguments:\n        a: first object\n        b: second object\n    ";if(a===b){return true}if(Array.isArray(a)&&Array.isArray(b)||a instanceof Object&&b instanceof Object){if(a.constructor!==b.constructor||a.length!==b.length){return false}if(Array.isArray(a)){for(i=0;i<len(a);i++){if(!eq(a[i],b[i])){return false}}}else{if(Object.keys(a).length!==Object.keys(b).length){return false}var ՐՏ_Iter0=ՐՏ_Iterable(a);for(var ՐՏ_Index0=0;ՐՏ_Index0<ՐՏ_Iter0.length;ՐՏ_Index0++){i=ՐՏ_Iter0[ՐՏ_Index0];if(!eq(a[i],b[i])){return false}}}return true}return false}function ՐՏ_in(val,arr){if(typeof arr.indexOf==="function"){return arr.indexOf(val)!==-1}return arr.hasOwnProperty(val)}function dir(item){var arr;arr=[];for(var i in item){arr.push(i)}return arr}function ՐՏ_extends(child,parent){child.prototype=Object.create(parent.prototype);child.prototype.constructor=child}function ՐՏ_print(){if(typeof console==="object"){console.log.apply(console,arguments)}}(function(){var __name__ = "__main__";var t0,a,i;t0=Date.now();for(i=0;i<1e6;i++){a=1}ՐՏ_print("petla for: ",Date.now()-t0,"ms")})();})();

My index.py looks like this:

t0 = Date.now()
for i in range(1000000):
    a = 1
print("loop for: ", Date.now()-t0, "ms")

Alexander Tsepkov

unread,
Sep 13, 2016, 3:31:40 PM9/13/16
to roberci...@gmail.com, RapydScript
This is not an error, you're getting exactly what you requested from the compiler. That's the compiled version of your code. If you want to run it you need to save it to a file (either via -o file.js or > file.js) and then put it in your browser. If you want to run it immediately (through node.js), use -x option to execute the code in place. Please see the compilation section of the README for more details: https://github.com/atsepkov/RapydScript#compilation.

Alexander Tsepkov

unread,
Sep 13, 2016, 4:48:55 PM9/13/16
to roberci...@gmail.com, RapydScript
Don't be afraid of spamming the mailing list, that's what it's there for. People who receive messages from it have voluntarily signed up for it, whether it's to learn more about the language, understand the roadmap or help other members. I will probably change to a different one later, one that's more user-friendly than Google Groups. Whatever questions you have may mean that other users who're starting out are struggling too but give up before asking others. Questions like this are an indicator of potential improvements I can make to the README (which by the way I definitely recommend you read when you have time, it's very good about explaining features and quirks of the language).

As far as your example, it works locally for me so I can't tell you immediately what's wrong off the top of my head. That is, both copy-pasting your above compiled code and compiling the message myself work for me. But let's debug it together (fyi, your problem is probably in #3):

1. The fact that the error appears for you with exactly same code makes me wonder if there is something different with your JavaScript engine, which browser are you using? Which version?

2. It's hard to understand where the error is when the code is compressed, could you run it with -p (pretty) option and then see which line it complains about?

3. The error claims to be on line 1, character 3232, yet there are only 1271 characters in the compiled code. This leads me to believe that there is something else in that same .js file, or perhaps it's picking up a different file instead.

Also, once the error is resolved, keep in mind that you will see no output, because you're only defining the greet function, but never actually calling it.

On Tue, Sep 13, 2016 at 4:27 PM, <roberci...@gmail.com> wrote:
I don't want to spam forum so I write here.

I don't know what I am doing wrong but RS is not working for me.

I have code directly from your github:

def greet():
    alert
("Hello World!")

then i use:

rapydscript index.py > index.js

This is output:

(function(){"use strict";var ՐՏ_Temp;function ՐՏ_Iterable(iterable){if(Array.isArray(iterable)||iterable instanceof String||typeof iterable==="string"){return iterable}return Object.keys(iterable)}function ՐՏ_bind(fn,thisArg){var ret;if(fn.orig){fn=fn.orig}if(thisArg===false){return fn}ret=function(){return fn.apply(thisArg,arguments)};ret.orig=fn;return ret}function range(start,stop,step){var length,idx,range;if(arguments.length<=1){stop=start||0;start=0}step=arguments[2]||1;length=Math.max(Math.ceil((stop-start)/step),0);idx=0;range=new Array(length);while(idx<length){range[idx++]=start;start+=step}return range}function len(obj){var length;if(typeof obj.indexOf==="function"){return obj.length}length=0;for(var i in obj){length++}return length}function eq(a,b){var i;"\n    Equality comparison that works with all data types, returns true if structure and\n    contents of first object equal to those of second object\n\n    Arguments:\n        a: first object\n        b: second object\n    ";if(a===b){return true}if(Array.isArray(a)&&Array.isArray(b)||a instanceof Object&&b instanceof Object){if(a.constructor!==b.constructor||a.length!==b.length){return false}if(Array.isArray(a)){for(i=0;i<len(a);i++){if(!eq(a[i],b[i])){return false}}}else{if(Object.keys(a).length!==Object.keys(b).length){return false}var ՐՏ_Iter0=ՐՏ_Iterable(a);for(var ՐՏ_Index0=0;ՐՏ_Index0<ՐՏ_Iter0.length;ՐՏ_Index0++){i=ՐՏ_Iter0[ՐՏ_Index0];if(!eq(a[i],b[i])){return false}}}return true}return false}function ՐՏ_in(val,arr){if(typeof arr.indexOf==="function"){return arr.indexOf(val)!==-1}return arr.hasOwnProperty(val)}function dir(item){var arr;arr=[];for(var i in item){arr.push(i)}return arr}function ՐՏ_extends(child,parent){child.prototype=Object.create(parent.prototype);child.prototype.constructor=child}(function(){var __name__ = "__main__";function greet(){alert("Hello World!")}})();})();

I create a html file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">

 
<head>
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 
<meta http-equiv="Content-Script-Type" content="text/javascript">

   
<script type="text/javascript" src="index.js">
   
</script>
 
</head>
 
<body>
   
<script type="text/javascript">
   
</script>
 
</body>
</html>

And I have this error in browser:
SyntaxError: missing ] after element list
in index.js 1:3232

Please help beacuse I must use your project and don't even know ho to print stupid hello world :(






Reply all
Reply to author
Forward
0 new messages