Nifty scroll script still not fast enough

I started writing this python script because I wanted to have a scrolling, fixed-width mpc status line in my dwm top bar:
#!/usr/bin/python3
from time import time
from subprocess import getstatusoutput
import sys
_ERROR_MSG = "Usage: scrollout [-l (int)] (cmd)"
def input_cmd(cmd,l):
if cmd == "":
s = sys.stdin.read()
else:
e,s = getstatusoutput(cmd)
if e: sys.exit(1)
s = s.replace("\n"," ")
s += (" " * int(l/3))
k = len(s)
t = int(k*(time()/k-int(time()/k)))
if k <= l: # static
return s
else: # scrolling
while len(s[t:]) < l: s += s
return s[t:t+l]
def run():
_arg = -1
length = 20
cmd = ""
enter_cmd = False
flags = ("-l")
for arg in sys.argv[1:]:
if enter_cmd or (arg not in flags and _arg not in flags):
cmd += arg + " "
enter_cmd = True
elif _arg == "-l":
try:
length = int(arg)
except:
print(_ERROR_MSG)
exit(1)
_arg = arg
output=input_cmd(cmd,length)
print(output)
run()
Since it uses time since Epoch to determine how much to scroll the text at any given moment it doesn't have to have any internal loops or run any longer than it takes to compute the output. You can simply call it like so:
scrollout -l 20 mpc current
alternatively
mpc current | scrollout -l 20
For example, in my statusbar script I have the following line:
mpc_status="|$(scrollout -l 20 mpc current) ($(mpc | grep -Eo '[0-9]*\:[0-9]*\/[0-9]*\:[0-9]*')) "
I find it works nicely, but is a bit slow. Not surprisingly, since it has to re-import all of it's libraries every time it gets launched. To be a truly useful script I imagine it would have to be able to finish it's task in less than one second. Right now it's just above that, ofcourse somewhat depending on what command is being piped through it. The big timestealer, however, is the module imports (according to the python profiler). Having a pretty limited experience of programming, I would like to know if anyone knows of a good workaround for this. Perhaps some way to speed up the module imports or to mimic some of the functionality of these modules without having to import them?
Cheers,
Mach

Thanks a lot guys! Glad to see such enthusiasm. :-)
juster: But I don't know C! :-) Oh well, I'm working nights, so I have time to learn.
Rewrote the script in python based on juster's algorithm:
#!/usr/bin/python3
from time import time
import sys
w = 20
if len(sys.argv) > 1: w = sys.argv[1]
s = sys.stdin.read().replace("\n","") + (" " * int(w/3))
if len(s) > w:
i = int(time() % w)
while len(s[i:]) < w: s += s
s = s[i:i+w]
print(s)
Then wrote a small bash script to run each of the different variants a hundred times and output their average execution time. Brace yourselves...
The script originally posted:
# mpc current | runtime10 scrollout.py | grep nanoseconds
Average run time: 0 seconds (205726798 nanoseconds)
Roughly the same script, frozen with cx_freeze:
# mpc current | runtime10 scrollout | grep nanoseconds
Average run time: 0 seconds (166651145 nanoseconds)
The perl script submitted by juster:
# mpc current | runtime10 ./perlscrollout | grep nanoseconds
Average run time: 0 seconds (24679770 nanoseconds)
The python script from this post:
# mpc current | runtime10 ./pythonscrollout | grep nanoseconds
Average run time: 0 seconds (190167249 nanoseconds)
So, the perl script still clearly stands out. Also, the new python script doesn't stand a chance compared to the old frozen executable, even though the script itself is proven faster than the original one. A frozen version of the new script will only get me so far in optimization - obviously, this is not a job for python.
Anyone care to prove me wrong?

Similar Messages

  • TS1702 All of a sudden I can only hear my podcasts in "fast" voices...not fast enough that I cannot understand what is said but still annoying!

    All of a sudden I can only hear my podcasts in "fast" voices...not fast enough to not understand but just fast enough not to be "real"

    Re: That garbage is unreadable.
    If you really want help, stop messing with the fonts and post so that others can read and offer suggestions.
    Or better yet... try a search, I'm certain you'll find a solution to whatever issue you're experiencing.
    I have found that many times it is the things that make you most angry that push you to action. This was the case here. Thank you for causing me to get so angry that I found the answer myself.

  • I am using an I-mac series 4 with a 2GHZ processor, I tried to convert to Lion and the rresponse was the chip was not fast enough, can I upgrade the chip  or do I need to purchase a new computer?o

    I am using an I-mac series 4 with a 2GHZ processor, I tried to convert to Lion and the rresponse was the chip was not fast enough, can I upgrade the chip  or do I need to purchase a new computer?

    Unfortunately yes, but Snow Leopard is still a great OS in the meantime so don't rush in too quick if you really don't need to.
    Good Luck
    Pete

  • Current xserve raid speed rate not fast enough

    guys,
    my xserve raid having problem when i edit HD project 1920*1080, its ok when i edit, but when i view all 30 mins movie, its hang when play till 5 mins of the movie, and keep on hanging after that, why is that? is it because the setting of my xserve raid got problem? read n write speed not fast enough?
    help plzzzz

    Both the write cache ticks should be on, The allow host cache flushing should be off. The use steady streaming mode tick should be off also. (So the pattern of th ticks is: on, off, on, off)
    The stripes stting depends on the cache size of the disk. On disks with lower than 16 MB, the 8 stripes setting is right. On disks from 16MB cache you can use the 128 stripes setting (In general the 750 GB disks have 16 MB, the others have lower).

  • Scroll-bar STILL not working right in 10.6.1 update

    The scroll-bar is STILL NOT working right in the 10.6.1 update.  I am not able to click anywhere on the scroll-bar and have the navigation go there.  I have to drag the blamed thing where ever I want it to go.  This problem only exist with the recent iTunes 10.6.x updates. 

    I have the new MacBook Pro 2010, Dell 3007WFP. No mirroring problemsat that or higher res. I was chagrined, however, to find out that while my 2007 15" MacBook Pro had no problems driving the Dell 30", the 2010 17" requires the Dual Link cable for $100 extra. ;>(
    But it works fine.
    ak

  • FCP WITH DUAL QUAD MAC - PROGRAM NOT FAST ENOUGH

    Hello, could anyone tell me the reason my new mac "Dual Quad (8 processors) 3.0 w/16gb or ram is not even blinking when I render projects in FCP/Motion? The processors are hardly working and I don't see much difference in overall speed from my previous mac or even my macbook pro. Is there reason the software isn't utilizing the full potential of this mac?

    Well, a ram disk treats ram as though it is swappable memory exactly like virtual memory space on the system hard drive. The only difference is the RAM is orders of magnitude faster.
    Back in the early PC days there was the 128k (or something like that) memory limit. To get around this all sorts of memory extender programs were written (QEMM was the best known). iirc they worked by simply swapping memory blocks from the base space to a higher (extended) space. To the hardware there was only the base amount. Ah, the bad memories flood back ...
    x

  • Firefox insists I install latest flash player -FlashP says my P3 735MHz 1M RAM not fast enough! So what to do? I won't be getting a new pC!

    Running Win XP SP2
    DISPLAY ADAPTER = NVIDIA GeFORCE2 MX/MX400
    PCI BUS 1

    Ignore Mozilla's '''''recommendation''''' about upgrading Flash. It's not mandatory for using Firefox.

  • MBP 2.4 Not strong enough? Dropped frames? Tutorial Files

    I just got the book FCP Editing (PEACH PIT), editing monk, sea world and commercial.
    Keep getting errors that DISK not fast enough (running from external HD), or too many RT (turned it to SAFE=still got error), any idea? The whole project is in red, perhaps I don't have a setting set up correct? Surely this system is CPU FPU fast enough.
    Advice?
    Thanks

    Perhaps your settings aren't correct.
    What is the format of the media, and what is the flavor of your 'external HD' -- FW400? USB? FW800?
    What are your sequence settings?
    Welcome to the forum... we'll get this straightened out!!
    CaptM

  • ITunes not loud enough

    I have a Toshiba P205 notebook. All the drivers are up to date. When I listen to podcasts on iTunes, I have to turn the volume all the way up, set the preamp to max, everything, and it's still not loud enough. When I run the 'test' app from Realtek, it's loud enough to break glass. What's up with iTunes?
    Same thing on my iPhone 3GS. Just not loud enough.
    Ideas?

    I have to turn the volume all the way up, set the preamp to max,
    What "preamp"?
    iTunes has its own volume separate from the system volume. Top left under the Play button.

  • Powerbook 1ghz - fast enough for divx?

    I have an opprotunity to get a great deal on a titanium 1ghz powerbook, which i would like to use as a portable personal media center... im curious if anybody has experience using one to watch divx .avi files using toast 7's divx codec? Im sure i could use VLC and it would work fine, but if possible i'd like to use quicktime to watch things.
    I know that there's a careful balance between the graphics processor and CPU to be successful with playback. I have a dual 500 with a radeon 9000 and it is not fast enough to watch divx files without serious skipping. However that same graphics card in a dual 1.25ghz G4 played them easily. The 1ghz has a 64mb radeon 9000 (presuming mobility version) but Im not sure if a 1ghz processor would be enough to play them successfully.
    I would appreciate anybody's opinions on this matter. Thanks for all your help.

    Hi Henry,
    Have you been to www.divx.com? Lots of info there and a different app that may work better for your purposes than Toast. Looks like the 1GHz would have plenty of power for it too:
    DivX for Mac
    Minimum System Requirements:
    500 MHz G4 or faster
    QuickTime 7
    Mac OS X 10.3.9
    Recommended System Requirements:
    600 MHz G4 or faster
    QuickTime 7 or later
    Mac OS X 10.4 or later
    DivX Video-on-Demand
    Minimum System Requirements:
    600 MB available hard drive space
    Broadband Network Connection (512 kbps or higher)
    Internet Explorer 5, FireFox 1
    Graphics card with 64MB of RAM or more and supporting 24-bit color or better for DivX High Definition profile video
    Recommended System Requirements
    800 MB available hard drive space
    Broadband Network Connection (512 kbps or higher)
    Internet Explorer 6, FireFox 1.5
    Graphics card with 64MB of RAM or more and supporting 24-bit color or better for DivX High Definition profile video

  • First time user of Mac(Book Air) and mouse sensitivity not high enough

    First time user of Mac(Book Air) and one thing that has bothered me is when I use a generic USB mouse and I've set the tracking speed/sensitivity to the max and it's still not high enough and it just irritates me to no ends. Am I not doing something properly being a first time mac user? Thanks in advance for any response.

    Depends on the mouse.  "Laser" with shorter wavelengths are far too sensitive for me and need to be dialed down.  IR is worthless in my opinion because they cannot move properly across a black/white checkerboard.
    I actually (to my great surprise) love using the trackpad and hate to use the mouse because trackpad keeps my thumbs directly between my hands while typing.  But that is just me.

  • G4 Sawtooth fast enough for streaming audio?

    I have a G4 Sawtooth (500mhz, 1Gb ram) and like to use it to host and stream audio via a quicktime player. My upload is 386kb per sec. Is this unit fast enough?
    probably there are buffers in the quicktime player or other if it's slow?

    I can't find any numbers, but I can't imagine that's not fast enough. I didn't stream, but via Airport played several songs stored on another Mac using QT Player.

  • HT4528 sound is not loud enough

    My ringtones for phone calls is not loud enough.  Is there a way to increase this other than the norm?

    To turn the volume up, go to Settings > Sounds and slide the volume bar to the right.
    Press the volume + button when you're on your home screen to turn the sound up.
    Double-tap your Home button and flick your finger from left to right TWICE and slide that bar to the right
    Remove any cases that you have and test it also
    If its still not loud enough for you after doing these, you can reset all settings (Settings > General > Reset). If its still not quite what you want, restore it as new in iTunes on your computer or take it to the Apple Store http://support.apple.com/kb/HT1414?viewlocale=en_US

  • Airport does not connect fast enough after sleep on Mac OS X Snow Leopard

    Ever since installing Snow Leopard, I have noticed that my Airport will not connect fast enough when I am waking from sleep. I sometimes have to wait upwards of 30 seconds after waking from sleep so Airport can connect and I can begin using the Internet. I notice that Airport says it is connected, but when I launch Safari, I get the "Not Connected to Internet" error message. I have tried removing the com.apple.airport.preferences.plist file to no avail. I have also tried Archiving and Installing and the issue is still there. Any ideas or fixes? Thanks.

    Those seem to happen a lot when you have stealth mode enabled (Security System Prefs, Firewall, Advanced). For me, they all seemed to be innocuous so I turned it off. I don't think this would affect your connectivity, though.
    You should also see it negotiate the connection. Is there anything that looks like it is repeating or retrying or other disconcerting messages? Here's an example of mine waking:1/5/10 8:27:36 PM kernel Wake reason = USB1
    1/5/10 8:27:36 PM kernel System Wake
    1/5/10 8:27:36 PM kernel vmmon: powerStateDidChange flags=0x82 (state 4)
    1/5/10 8:27:36 PM kernel Previous Sleep Cause: 0
    1/5/10 8:27:36 PM kernel USB (UHCI):Port 2 on bus 0x1d has remote wakeup from some device
    1/5/10 8:27:36 PM kernel EIR is not supported.
    1/5/10 8:27:37 PM kernel en1: 802.11d country code set to 'US'.
    1/5/10 8:27:37 PM kernel en1: Supported channels 1 2 3 4 5 6 7 8 9 10 11 36 40 44 48 52 56 60 64 149 153 157 161 165
    1/5/10 8:27:38 PM kernel AirPort: Link Up on en1
    1/5/10 8:27:38 PM kernel AirPort: RSN handshake complete on en1
    1/5/10 8:27:38 PM configd[14] network configuration changed.

  • TS3694 MY IPhone will not let me back up or download the IOS 7 update because it says i do not have enough storage space. But I deleted all of my apps and pictures just to update it and it still wont let me do it. What should I do?

    MY IPhone will not let me back up my phone or download the IOS 7 update because it says i do not have enough storage space. But I deleted all of my apps and pictures just to update it and it still wont let me do it. What should I do?

    Contact iTunes to re-enable your account at expresslane.apple.com
    You can reach them by chat or email.

Maybe you are looking for