• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading source file & writing to target file Vs copying source file to target file?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.Reading a source file char by char by using BufferedReader, FileReader and writing to a target file char by char by using BufferedWriter, FileWriter.
2. Copying all content of a source file to target file by using Files.copy(source, target, REPLACE_EXISTING)

Which one is better in performance ?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. Try it and see.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes tried, It relies on the application requirement if want to just copy whole content of a file then second is good and if want to deal with each char of source file then first one.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how did you time it?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:Which one is better in performance ?

In terms of memory, cpu cycles, clock time, disk I/O, a developer's time spent writing the code...

there are many ways to measure "performance". Without you defining what EXACTLY matters, all anyone can do is guess.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ fred If developer want to check how many times a specific character is present, at what location or if want to write just vowels to another file and all these kind programs of dealing with chars can be done using FileReader. If want to just copy whole content as it is then Files.copy(source, target, REPLACE_EXISTING) . This is general requirement. but
In terms of memory, cpu cycles, clock time, disk I/O, a developer's time spent writing the code... there are many ways to measure "performance". Without you defining what EXACTLY matters, all anyone can do is guess
about this I don't know how to check its performance in terms of memory, time, clock time etc because I'm just practicing simple code and not implementing any big application where I need to copy content of files in thousands and more, then and then only I can face the performance issue.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Campbell I really have no idea how to measure its performance in terms of time.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the methods in the System class which time things. Before and after. You cannot do something once and time it. You would want to repeat it 1000000 times and see how long it takes. If you are copying files, create two nonsense folders to put your source and destination files in. Then you can delete the entire folders. In the case of files maybe 1000000 files it is too many.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Campbell
1000000 files
A big number! yes I'll definitely do that...
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Char by char:
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
copy content:
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this source file, It has paragraphs and white spaces, The source file size is 1.00 kb so 100000*1 kb = 97.6 MB,

Copying a source file as it is to target files took much time compared to writing a source file char by char to target files. But in char by char all the content of a source file is written in one line but in copying it copies source file's content like paragraphs and line gaps ( I mean the text format ) as it is in target files.
The size of target file of char by char is 94.9 MB, no data missing but removes white spaces.
The size of target file of copying is 97.6 MB, no data missing

char by char took 273129 milliseconds
copying took 403183 milliseconds
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote: about this I don't know how to check its performance in terms of memory, time, clock time

it's ok that you don't know HOW to do it...the question I am asking is WHICH do you care about?

Much of software design involved trade-offs. You can sometimes increase the speed if you use more memory. You might reduce memory if you use more disk i/o at the cost of having to wait for that to happen, etc.


Whenever you talk about "performance", the question is "which kind of performance is most important to you?" Think about cars. A top fuel dragster is incredibly fast at going a 1/4 mile...at the cost of burning a LOT of fuel. A Tesla uses no petroleum, but has a limited 200 mile range. A school bus can transport a lot of people at the same time, but does not get the same fuel efficiency of a Prius (but better than a drag racer).

So...what is most important to you - cpu cycles, memory, clock time, or something else? What you know WHAT you want to measure, then you figure out HOW to measure it.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought writing char by char didn't consider white spaces and writes on same line so might not have taken that much long time as much copying took but this time I put characters in one line no gap in any characters in source file and the size of source file is 500 bytes.
char by char took 169720 milliseconds.
copying took 392976 milliseconds. here also copying took much time, may be because copying also set same attributes(hidden, readonly etc) to target files as source file.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No actually I was just trying to check which one takes less time. It is not like I'm using it in my project or somewhere. ya but I got your point
 
Dave Tolls
Ranch Foreman
Posts: 3058
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your "char by char" one you are not doing it char by char, you are copying line by line. And those lines do not have the new line character in them (it's ignored by the readLine method).
Also, you don't appear to be closing the reader at all?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are not doing it char by char, you are copying line by line.
yes your correct, I didn't notice that & yes I should've closed reader.
those lines do not have the new line character in them
oh that's why its writing on a line thank you anyway I should make this resolved as I moved ahead to learn JSP & Servlet. once again thank you Dave.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote: . . . I should make this resolved . . .
What makes you think it is resolved?

Do you think the time to write depends on setting file attributes? Or simply on finding suitable space on your hard drive for the writing? Or is it the time spent reading? Or does it vary from run to run? I don't know myself.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think it is resolved?
as I was looking for
Which one is better in performance ?
then came to know performance means
In terms of memory, cpu cycles, clock time, disk I/O, a developer's time spent writing the code...there are many ways to measure "performance"
but I didn't have any purpose at the beginning because I was just practicing and guessing as fred said
there are many ways to measure "performance". Without you defining what EXACTLY matters, all anyone can do is guess
, then realized that I should know first what matters me, after knowing performance measurement ways then thought to measure performance in time, both takes to complete this task.
In read and write line by line:
If files don’t exist(means create files & write) then it took 257606 milliseconds.
If files already exists (means erase previous content & overwrite new) then it took 233240 milliseconds.
In copying files:
If files don’t exist (means create files & copy) then it took 463059 milliseconds.
If files already exists then we have to use
Which replaces existing content & writes new because if we use Files.copy(sourceFile, targetFile,COPY_ATTRIBUTES); when files already exist then gives FileAlreadyExistsException as it creates then writes contents.
took 411114 milliseconds.

If we observe, little more time spent on creating files in both, If files are already created then reduces total time spent in both.
Copying takes much time if you make an analogy between reading and writing line by line and copying.
Conclusion: I found reading & writing line by line ( as usual made mistake it's reading line by line not char by char Dave made me notice ) takes less time than copying files. So reading & writing line by line performs better than copying files in terms of time.

as my question was about just performance( later choose in terms of time). I felt it's resolved. yes if we ponder why copying takes more time than reading & writing line by line then it's not resolved.

Do you think the time to write depends on setting file attributes?
I don't think so because I didn't create target files in advance and used same source file in both ways. Both of them have to create target files with same attributes (If target files are not present then both creates them as in copying I first used which creates and copies to files then used which overwrites existing files & measured performance of both copying types in terms of time).

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simply on finding suitable space on your hard drive for the writing
perhaps, because if there is less space on hard drive then performance may differ.
is it the time spent reading
may be, difficult to find reading performance of copying in terms of time, we can find reading performance of read line by line in terms of time so not sure .
does it vary from run to run
yes it varies from run to run. I think it also relies on your computer, if there is too much work load
on your system like using some kind of applications or works are going on which is heavy and took much space then takes more time but in both ways.

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote: . . . then it took 257606 milliseconds.
. . . it took 233240 milliseconds.
In copying files:
. . . 463059 milliseconds.
. . . took 411114 milliseconds.
. . .
Are those times actually different? I suspect 257 and 233 are not significantly different.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes its minuscule, not much difference.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact whenever I have tried that sort of timing, I find the technique which takes 257′ occasionally runs faster than the tchnique taking 233′.

I am glad we kept you working at this thread because I think you have learnt lots from it
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes learning lot of things and this community is the reason behind it
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!