|
From: Jeff E. <jem...@fr...> - 2005-02-17 16:21:01
|
Because it does a physical I/O operation for every byte. You need to wrap the file streams with buffered streams, for example: bi = BufferedInputStream(FileInputStream(File(filename))) Lauri Lehtinen wrote: > Setting to use the binary mode worked! > > Does any one know why java variant of this in jython performs very poorly? > > Here the basic java variant: > --- > from java.net import * > from java.io import * > def copy(filename): > bi = FileInputStream(File(filename)) > bo = FileOutputStream(File(filename+".copy")) > b = [1000000] > while 1: > l = bi.read(b) > if l == -1: > break > bo.write(b) bo.close() > bi.close() > return 1 > if __name__ == "__main__": i = raw_input("Give file:") > copy(i) > --- > That berforms only about 10kB/s, here one that uses buffers and performs > better: > --- > from java.net import * > from java.io import * > def copy(filename): > bi = BufferedInputStream(FileInputStream(File(filename))) > bo = BufferedOutputStream(FileOutputStream(File(filename+".copy"))) > while 1: > l = bi.read() > if l == -1: > break > bo.write(l) bo.close() > bi.close() > return 1 > if __name__ == "__main__": i = raw_input("Give file:") > copy(i) > --- > This performs about 600kB/s what isn't good at all, that should be much > better. I tried also by using static buffer size but that performed even > poorly for some reason. Why the performance is so bad? > > Sincerely, > Lauri Lehtinen > > Kent Johnson wrote: > >> Lauri Lehtinen wrote: >> >>> Hi, >>> >>> I just noticed this, when I have this kind of code: >>> --- >>> def copyFile(filename): >>> bi = open(filename) >>> bo = open(filename+'.copy', 'w') >>> b = 1000000 >>> while 1: >>> l = bi.read(b) >>> if len(l) == 0: >>> break >>> bo.write(l) >>> bo.close() >>> bi.close() >>> return 1 >>> i = raw_input("Give file:") >>> copyFile(i) >>> --- >>> and e.g. copy mp3 file with it the .copy mp3 file is somehow mixed >>> and not working as it should, but when I run this with plain python >>> it works just as it should. How to fix or workaround this? Is thus >>> known bug? >> >> >> >> Maybe you need to use binary mode? >> bi = open(filename, 'b') >> bo = open(filename+'.copy', 'wb') >> >> How do the two files differ? >> >> Kent >> >>> >>> I have tried to make the same using java's FileInputStream and >>> FileOutputStream but then the performance is then very bad (about >>> 500kB/s). Is that also known issue? >>> >>> Sincerely, >>> Lauri Lehtinen >>> >>> >>> >>> ------------------------------------------------------- >>> SF email is sponsored by - The IT Product Guide >>> Read honest & candid reviews on hundreds of IT Products from real users. >>> Discover which products truly live up to the hype. Start reading now. >>> http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click >>> _______________________________________________ >>> Jython-users mailing list >>> Jyt...@li... >>> https://lists.sourceforge.net/lists/listinfo/jython-users >>> >> >> >> >> >> ------------------------------------------------------- >> SF email is sponsored by - The IT Product Guide >> Read honest & candid reviews on hundreds of IT Products from real users. >> Discover which products truly live up to the hype. Start reading now. >> http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click >> _______________________________________________ >> Jython-users mailing list >> Jyt...@li... >> https://lists.sourceforge.net/lists/listinfo/jython-users >> >> >> > > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users |