Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6929003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:15:44+00:00 2026-05-27T11:15:44+00:00

Below is my benchmarking code. First I try to serialize a single object. Then

  • 0

Below is my benchmarking code. First I try to serialize a single object. Then I try to do the same thing one million times. I was expecting a proportional increase in timing. Instead I get

1323048944117 1323048944131
1323048944117 1323048944210

14ms for one object
93ms for 1mil objects

What is happening here? I’m most worried about the time for single object conversion which I’m hoping to get to the submillisecond level.

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Pojo implements Serializable {
    String blah = "11111111111111111111aaaaaaaaaaaaaaaaaaaa";
}

public class SerializeTest {
    public static void main(String[] args) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        Pojo pojo = new Pojo();
        long start = 0;
        long end1 = 0;
        try {
            out = new ObjectOutputStream(bos);
            start =  System.currentTimeMillis();
            out.writeObject(pojo);
            end1 = System.currentTimeMillis();
            for (int i=0;i<1000000;i++) {
                out.writeObject(pojo);
            }

        } catch (Exception ex) {

        }
        long end2 = System.currentTimeMillis();
        System.out.println(start + " " + end1);
        System.out.println(start + " " + end2);

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:15:45+00:00Added an answer on May 27, 2026 at 11:15 am

    Ultimately @duffmo is right that your results are 98% about the problems with doing micro testing in Java than it is about serialization. He posted a good link to read.

    Another factor that is confusing your test is that serialization is smart. Writing the first object writes all of the class and field information and the data. The 2nd to the nth write of the same object instance is only writing a reference to the first object. The first object looks to be ~120 bytes and each remaining one is only 5 bytes. It’s also going to be tons faster because it does a lot less.

    My test did the 1st object in 8ms, the next 1000 objects in 5ms, and then a million objects in 2000ms. This shows you mostly how fast serialization is. It also shows you that Java does some magic real-time optimizations that cause drastically non-linear speed graphs.

    If you are trying to do some serialization speed testing I would make your Pojo class generate some random numbers (or a random string) and I would compare the serialization of 1000 objects to a million to have a better comparison of per-object serialization times. Either that or start the timing after you have written 1000 objects or something. For example, writing a different object produces a file that is ~10 times larger but only took 5600ms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below are two ways of reading in the commandline parameters. The first is the
Below is the code of a simple html with a table layout. In FF
Below is my innermost loop that's run several thousand times, with input sizes of
Below is the beginning of a chunk of SPIM code: .data a: .space 20
I'm benchmarking some SSE code (multiplying 4 floats by 4 floats) against traditional C
Below is my code I am trying to get to work but I really
Below is the code to fill a list box in a VBA application :
Below is my code that works fine on IE 8 but it doesn't work
Below is the code I'm using to run the query, parse the result set,
Below is the code for my player which will automatically generate a playlist.xml file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.