Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

any replace is slow

Help
quietseb
2012-03-23
2013-08-22
  • quietseb
    quietseb
    2012-03-23

    Hi

    I've using notepad++ for a few years but only recently took the time to try pythonscript.

    I was hoping to automate cleanup of trace files. Right now I have a light trace file (only about 3000 lines).
    Every line looks pretty much like this:

    PSAPPSRV.15644       1-284    18.05.28    8.533095 Cur#1.15644.PAY89DEV RC=0 Dur=0.000474 COM Stmt=SELECT VERSION FROM PSVERSION WHERE OBJECTTYPENAME = 'SYS'
    

    I'd like to remove everything till Dur=0.000474

    Directly with notepad++ I'm using this regexp :

    PSAPPSRV.*Dur=[.0-9]+
    

    The result is about instantaneous.

    I tried the following python script:

    editor.pyreplace("PSAPPSRV.*Dur=[.0-9]+", "")
    

    Or

    editor.rereplace("PSAPPSRV.*Dur=[.0-9]+", "")
    

    both take about 30 seconds to complete !
    Actually, even a simple replace

    editor.rereplace("PSAPPSRV", "")
    

    takes the same time to complete.

    I've seen the 1-year old "pymlreplace - very slow" thread in which the OP says "(about 100 lines per second for each editor.pymlreplace…)", that's about what I get.
    I'm wondering whether this is expected ? Would the difference in speed between "native npp replace" and "python script replace" be justified by the fact we're in a plugin ?

    Thank you for any input.

    Cheers,
    seb

     
  • quietseb
    quietseb
    2012-03-23

    btw, I'm using PythonScript 0.9.2.0 on Notepad++ 5.9.8, win7-32, a recent setup.
    Thanks

     
  • MMTsuchi
    MMTsuchi
    2012-03-29

    Hi, I am face to the slow pyreplace too. I takes near 10 lines/s which is very slow
    I'm using PythonScript 0.9.2.0 on Notepad++ 5.9.8, winXP-media-center 32bits.
    No tried before. I am new to pyhonscript.

     
  • Yes, I can confirm it's slow.  For pyreplace(), this is (as far as I can tell) down to swapping in and out of python context, and calling essentially a python function per line.  For rereplace(), this should actually be as quick as N++ itself, however it's not. I'm working on a further plugin that specialises in search and replace, and that is already 5 times quicker than native n++. When it's done, I'll port the fast code back to Python script.

    This will only affect rereplace(), although with the new regular expression support in N++ 6, that means you'll only need pyreplace() when you've actually got a function to replace the content. 

    I'll certainly see what I can do with the pyreplace() to make it a bit quicker - it may be faster, for instance, to actually do the whole replacement in python, and not switch in and out.  Alternatively it may be the way pyreplace() uses Scintilla that makes it slow.  I would expect some form of performance increase for pyreplace() in the next version of python script, which is long overdue.  

    Cheers,
    Dave.

     
  • quietseb
    quietseb
    2012-05-23

    Hello Dave,

    thank you for your answer. It's good news for us that you're already working on updating the search and replace features.

    will be waiting :-)

    cheers,
    seb

     
  • un1q
    un1q
    2013-08-22

    I see, that one year later pyreplace is still to slow to by useful. I believe the problem is caused by repeatedly screen refreshes. My solution is to use standard python regex functions instead, for example:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    #!/usr/bin/python
    import re
    
    editor.beginUndoAction()
    text = editor.getText()
    text = re.sub(re.compile('\r\n\t[^\r\n]*\r\n', re.M), '\r\n', text)
    text = re.sub(re.compile('\r\n(\r\n)+', re.M), '\r\n', text)
    text = re.sub(re.compile('\r\n +', re.M), '', text)
    text = re.sub(re.compile('\r\n(\[[^\r\n]*)\r\n', re.M), '\r\n\\1\t\r\n', text)
    text = re.sub(re.compile('\r\n([^\[])', re.M), '\\1', text)
    editor.clearAll()
    editor.addText(text)
    editor.endUndoAction()
    

    I hope somebody will find this tip useful.