M
Michael Preminger
In association with a Programming course, I need to explore the
performance differences between using binary
files (serialized object representation saved to a file) and text files
(text representations of the same object).
I have made a simple experiment:
I created a class Person with some simple attributes (code given)
below). I gave it a text representation (toString()), that covers
everything needed for a unique reconstruction of an object.
I compare the following scenarios:
1. Saving 20000 replica of the text representation of a Person object
into a text file and then reading them and constructing a person object
from the text representation 20000 times.
2. Saving the same number of replica of the serializable Person object
into a binary file and reconstructing the object the same number of time
from that representation.
I measure the performance time (using System.currentTimeMillis() before
and after and subtracting) of each action
What I see, to my surprize, is that the text representation is more
expensive in memory terms but much much faster to process than the
binary representation. This must mean that serialization /
deserialization of objects in a binary representation is slower than
constructing objects. My saving of space is due to the fact that I am
using the same class, and Java, therefore, only codes the class once
into the file.
My questions are:
1. Is my experiment (see code pieces below) valid and general enough to
compare the performances?
2. Are there cases, similar in spirit to the one above
where a binary representation would outperform a text representation
also in processing time?
(A complete code can be downloaded from here:
http://bibin.hio.no/vu/programmering/uke_19/TestLagring_1.java
----------------------------------------------------------
//Person.java - En klasse som modellerer en
// generisk person
package no.hio.bibin.michaelp.person;
public class Person implements java.io.Serializable
{
//felter (klassens egenskaper)
private String fornavn;//first name
private String etternavn;//last name
private String personnr;//id-number
private boolean kvinne;//gender (true for woman)
//accessor-metoder
public String getFornavn(){
return fornavn;
}
public void setFornavn(String fNavn){
fornavn=fNavn;
}
public String toString(){
String ret= "";
/*if (kvinne){
ret+="Fr. ";
}else{
ret+="Herr ";
}*/
String tf=kvinne?"true":"false";
ret+=etternavn+" "+fornavn+" "+personnr+" "+tf;
return ret;
}
public String getEtternavn(){
return etternavn;
}
public void setEtternavn(String eNavn){
etternavn=eNavn;
}
public String getPersonnr(){
return personnr;
}
public void setPersonnr(String pNr){
personnr=pNr;
}
public boolean getKvinne(){
return kvinne;
}
public void setKvinne(boolean kv){
kvinne=kv;
}
/*public Person(){}*/
/* public String toString(){
}*/
public double arbeidsLast(){
return 0;
}
public Person(String fNavn, String eNavn, String pNr, boolean kvn){
fornavn=fNavn;
etternavn=eNavn;
personnr=pNr;
kvinne=kvn;
}
public String getFodselsdato(){
return personnr.substring(0,6);
}
}
-----------------------------------------------------------------------
//Saving the binary representation
public static void lagreBin(int antallGanger) throws IOException{
oos=new ObjectOutputStream(new FileOutputStream(filebin));
for (int i=0;i<antallGanger;i++){
oos.writeObject(p);
}
oos.close();
}
//saving text representation
public static void lagreString(int antallGanger) throws IOException{
bw=new BufferedWriter(new FileWriter(filetext));
for (int i=0;i<antallGanger;i++){
bw.write(p.toString());
bw.newLine();
}
bw.close();
}
//reconstructing from text representation
public static void lesInnString(int antallGanger) throws IOException{
br=new BufferedReader(new FileReader(filetext));
for (int i=0;i<antallGanger;i++){
String ps=br.readLine();
String[] sa=ps.split(" ");
boolean kv=sa[2].equals("true");
Person p=new Person(sa[0], sa[1], sa[2], kv );
}
}
//reconstructing from binary representation
public static void lesInnBin(int antallGanger) throws IOException,
ClassNotFoundException{
ois=new ObjectInputStream(new FileInputStream(filebin));
for (int i=0;i<antallGanger;i++){
Person p= (Person) ois.readObject();
}
ois.close();
}
Michael Preminger
Høgskolen i Oslo
p48/R507
22 45 27 78
performance differences between using binary
files (serialized object representation saved to a file) and text files
(text representations of the same object).
I have made a simple experiment:
I created a class Person with some simple attributes (code given)
below). I gave it a text representation (toString()), that covers
everything needed for a unique reconstruction of an object.
I compare the following scenarios:
1. Saving 20000 replica of the text representation of a Person object
into a text file and then reading them and constructing a person object
from the text representation 20000 times.
2. Saving the same number of replica of the serializable Person object
into a binary file and reconstructing the object the same number of time
from that representation.
I measure the performance time (using System.currentTimeMillis() before
and after and subtracting) of each action
What I see, to my surprize, is that the text representation is more
expensive in memory terms but much much faster to process than the
binary representation. This must mean that serialization /
deserialization of objects in a binary representation is slower than
constructing objects. My saving of space is due to the fact that I am
using the same class, and Java, therefore, only codes the class once
into the file.
My questions are:
1. Is my experiment (see code pieces below) valid and general enough to
compare the performances?
2. Are there cases, similar in spirit to the one above
where a binary representation would outperform a text representation
also in processing time?
(A complete code can be downloaded from here:
http://bibin.hio.no/vu/programmering/uke_19/TestLagring_1.java
----------------------------------------------------------
//Person.java - En klasse som modellerer en
// generisk person
package no.hio.bibin.michaelp.person;
public class Person implements java.io.Serializable
{
//felter (klassens egenskaper)
private String fornavn;//first name
private String etternavn;//last name
private String personnr;//id-number
private boolean kvinne;//gender (true for woman)
//accessor-metoder
public String getFornavn(){
return fornavn;
}
public void setFornavn(String fNavn){
fornavn=fNavn;
}
public String toString(){
String ret= "";
/*if (kvinne){
ret+="Fr. ";
}else{
ret+="Herr ";
}*/
String tf=kvinne?"true":"false";
ret+=etternavn+" "+fornavn+" "+personnr+" "+tf;
return ret;
}
public String getEtternavn(){
return etternavn;
}
public void setEtternavn(String eNavn){
etternavn=eNavn;
}
public String getPersonnr(){
return personnr;
}
public void setPersonnr(String pNr){
personnr=pNr;
}
public boolean getKvinne(){
return kvinne;
}
public void setKvinne(boolean kv){
kvinne=kv;
}
/*public Person(){}*/
/* public String toString(){
}*/
public double arbeidsLast(){
return 0;
}
public Person(String fNavn, String eNavn, String pNr, boolean kvn){
fornavn=fNavn;
etternavn=eNavn;
personnr=pNr;
kvinne=kvn;
}
public String getFodselsdato(){
return personnr.substring(0,6);
}
}
-----------------------------------------------------------------------
//Saving the binary representation
public static void lagreBin(int antallGanger) throws IOException{
oos=new ObjectOutputStream(new FileOutputStream(filebin));
for (int i=0;i<antallGanger;i++){
oos.writeObject(p);
}
oos.close();
}
//saving text representation
public static void lagreString(int antallGanger) throws IOException{
bw=new BufferedWriter(new FileWriter(filetext));
for (int i=0;i<antallGanger;i++){
bw.write(p.toString());
bw.newLine();
}
bw.close();
}
//reconstructing from text representation
public static void lesInnString(int antallGanger) throws IOException{
br=new BufferedReader(new FileReader(filetext));
for (int i=0;i<antallGanger;i++){
String ps=br.readLine();
String[] sa=ps.split(" ");
boolean kv=sa[2].equals("true");
Person p=new Person(sa[0], sa[1], sa[2], kv );
}
}
//reconstructing from binary representation
public static void lesInnBin(int antallGanger) throws IOException,
ClassNotFoundException{
ois=new ObjectInputStream(new FileInputStream(filebin));
for (int i=0;i<antallGanger;i++){
Person p= (Person) ois.readObject();
}
ois.close();
}
Michael Preminger
Høgskolen i Oslo
p48/R507
22 45 27 78