Open xtvdata opened 3 years ago
The above Python code in bash is something like (not exactly the same, I know but the example fits the purpose)
val=`cat /sys/class/thermal/thermal_zone0/temp`;
echo "$val / 1000.0" | bc > /dev/null
Thus, preparing a couple of scripts cycling multiple times on the code you can do a not extremely accurate, but very simple benchmark by running them with the time
command.
If you run both of the scripts with 1 execution you get: Python: real 0m 0.06s user 0m 0.05s sys 0m 0.00s Bash: real 0m 0.00s user 0m 0.00s sys 0m 0.00s Obviously bash is the king...
if you run it with 10 executions you get: Python: real 0m 0.06s user 0m 0.06s sys 0m 0.00s Bash: real 0m 0.05s user 0m 0.03s sys 0m 0.02s Bash still rules, but not as much as before...
if you run it with 25 executions you get: Python: real 0m 0.06s user 0m 0.06s sys 0m 0.00s Bash: real 0m 0.13s user 0m 0.06s sys 0m 0.07s Bash now starts to be slower than Python...
if you run it 50 times you get: Python: real 0m 0.07s user 0m 0.06s sys 0m 0.00s Bash: real 0m 0.27s user 0m 0.13s sys 0m 0.14s Bash is significantly slower than Python...
100 times: Python: real 0m 0.07s user 0m 0.07s sys 0m 0.00s Bash: real 0m 0.54s user 0m 0.28s sys 0m 0.27s Python rules...
Basically the big issue is to load Python when you start the script, if you need to load many commands you have time to load Python and do everything else very quickly. Btw the numbers above are NOT running byte-code Python, but I guess it won't change much as in this small script there is not much to compile anyway. The important thing is to avoid starting the python interpreter more than once.
Note: I didn't "time" the Perl solution since there was no Perl installed on my AX88u, but I'm quite sure it's going to be faster then bash much quicker than Python (which however for some reasons was already there).
Hello and thanks for the infos. I have made a lot of changes in the last days. Need some more testing, but i hope to get a new version out soon.
BTW I was curious, thus while I'm not a Python coder, I've been trying to rewrite part of your code in pure Python. Just to see what was the outcome...
plain execution of mod_basic.sh: real 0m 3.75s user 0m 0.75s sys 0m 0.91s
plain execution of mod_basic.py (compiled live on execution): real 0m 1.26s user 0m 1.07s sys 0m 0.07s
direct execution of precompiled Bytecode version of mod_basic.py real 0m 1.09s user 0m 0.95s sys 0m 0.06s
Now, the interesting thing is that besides the python code seems to be much faster, is that it shifts most of the load from "system" to "user".
Notes:
P.S.: rewriting" mod_basic.sh" in Python I've noticed that you already had the info about the CPU temperature... :-)
Sounds excellent. Yes, Python can be faster, but sadly I'm not a python dev. For some basic stuff it's ok, but because of the lack of experience with it, I would need much more time to learn while coding. Do you have your code online here, so that i can try it?
Sorry for the delay... I'll need a couple of days to roundup a few corners (I'm not a Python dev myself ;-) ) and fix a couple of things in Grafana, then I'll try load the .py files.
you dont have to stress yourself. For me its more for education. Because im not a python dev, i dont think that a want to switch to python.
I actually changed a lot of the code on my installation, but my setup is quit unusual, because i switched to pfsense for routing, dhcp awo. Im only using the asus router for the wifi mesh. So its hard to test the stuff related with ip/mac/hostname.
Hi, concerning the topic "how to get the CPU temperature" here is a way to do it on AX88u (here is a simplified Python function):
Note: if you decide to implement it in Python you should add some error management to the above code depending on how you use it, e.g. in case the file is not there (in AX88u it should be always there, but on a different router it could be in another place or just be missing).
P.S.: I've seen other topics against the use of Python on the router due to higher resources usage, however the real point should not be to avoid the use of Python, but to try to avoid calling python scripts as you do with other binary executables in bash scripts (quote: “Don’t cross the streams.”). Bash in itself is fast and light, however the burden starts to be heavy when you call multiple external executables in the script (even compiled binaries). Every single external script/command called involves, besides disk/cache access, allocating and running an additional process, if in a script you call many times external programs to perform small tasks the overhead is significant and it would probably be much more effective and efficient to run everything in a single Python (even better from byte-code) or Perl script. To sum up:
So if you can do it with native bash involving a very limited number of external commands in it, go for it. If you need a lot of external commands I'd say to consider Perl, which is very fast in parsing text and files. If you need Perl libraries I'd say to give it a shot with Python and its native features. Mostly the decision depends on how complex are the scripts and the hardware on which they will run.