A rough comparison between Java IO and NIO Object serialization and file writing Efficiency

Source: Internet
Author: User

A rough Test

S = List <Map <String, Object> table = ArrayList <Map <String, Object> (I = 0; I <1; I ++ Map <String, object> map = HashMap <String, Object> Double d1 = Double d2 = Double d3 = map. put ("name" map. put ("id" map. put ("age" ByteArrayOutputStream outStream = ObjectOutputStream objectOutputStream = FileOutputStream fout = FileOutputStream ("test.txt" FileChannel cha = ByteBuffer bb = System. out. println ("successful" + (System. currentTimeMillis ()-}}}NIO s = List <Map <String, Object> table = ArrayList <Map <String, Object> (I = 0; I <1; I ++ Map <String, object> map = HashMap <String, Object> Double d1 = Double d2 = Double d3 = map. put ("name" map. put ("id" map. put ("age" FileOutputStream outStream = FileOutputStream ("test.txt" BufferedOutputStream bo = ObjectOutputStream objectOutputStream = System. out. println ("successful" + (System. currentTimeMillis ()-}}}IO

The results of the for test are adjusted as follows (the name is followed by the average time ):

Time 1 10 100 1000 10000 100000 1000000
OtoF

Avgb 15.5

31

15

15

16

16

15

16

16

15

0

Avgb 15.5

16

16

15

15

16

15

15

16

15

16

Avgb 32.6

31

29

31

32

31

47

31

32

31

31

Avgb 84.1

83

81

79

86

80

92

82

86

83

89

Avgb 262.9

361

245

275

248

244

248

248

249

259

252

Avgb 2132.7

2246

2231

2075

2278

2106

2075

2075

2075

2091

2075

Exception in thread "main" java. lang. OutOfMemoryError: Java heap space

OtoBtoF

Avgb 23.2

31

31

31

16

15

16

31

31

15

15

Avgb 23.3

16

15

15

31

31

15

16

31

32

31

Avgb 35.8

47

31

47

32

31

31

31

31

46

31

Avgb 86.5

93

89

84

86

88

85

85

88

83

84

Avgb 259.4

266

266

279

251

260

257

256

252

254

253

Avgb 2174.7

2169

2153

2169

2372

2137

2168

2106

2153

2152

2168

Exception in thread "main" java. lang. OutOfMemoryError: Java heap space

The test results show that the channel is time-consuming than the stream. However, it is said that the channel reads and writes in blocks and the stream is in bytes, so the channel is faster. Therefore, I initially thought that the data volume was not large enough. As the data volume increases, the channel method did not see any advantages. However, maybe the Code itself is not suitable. If you have any better method, please comment on it.

In addition, I also checked whether BufferedOutputStream is efficient. For the preceding stream method, BufferedOutputStream is indeed closer to 1 than it is not.

S = List <Map <String, Object> table = ArrayList <Map <String, Object> (I = 0; I <100000; I ++ Map <String, object> map = HashMap <String, Object> Double d1 = Double d2 = Double d3 = map. put ("name" map. put ("id" map. put ("age" ByteArrayOutputStream outStream = ObjectOutputStream objectOutputStream = [] B = RandomAccessFile raf = RandomAccessFile ("test.txt", "rw" FileChannel cha = MappedByteBuffer mbb = cha. map (MapMode. READ_WRITE, 0 FileLock fl = System. out. println ("successful" + (System. currentTimeMillis ()-}}}Map

Even if the above memory ing file is used, the effect is not satisfactory! The reason is unknown. When RandomAccessFile is used to obtain the channel for ing, the ing cannot be performed, and the map reports that the readable channel cannot be found.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.