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 6537805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:39:39+00:00 2026-05-25T10:39:39+00:00

In an online judge programming contest problem I am required to output up to

  • 0

In an online judge programming contest problem I am required to output up to 50,000 lines in under 1 second via standard out (in addition to reading in up to 200,000 pairs of integers which I used a buffer for). My logic seems to be correct but I keep getting my submissions turned down for exceeding the 1 second runtime. I stripped down my code logic to just output a constant string and it still exceeds the time limit.

Is there a faster way to output than using System.out.println(String s)for every line of output?

  • 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-25T10:39:39+00:00Added an answer on May 25, 2026 at 10:39 am

    I would use a single System.out.print call (or the least that makes sense which is to be found out via benchmarking) like this:

    String str = "line1\nline2\nline3\n ...";
    System.out.print(str);
    

    Edit:

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 500000; i++) {
            sb.append(i).append("\n");
        }
        String str = sb.toString();
        long nt = System.nanoTime();
        System.out.print(str);
        nt = System.nanoTime() - nt;
        System.out.print("\nTime(ms): " + (double)nt / 1000000);
    

    sb.toString() is not a free operation.

    The above takes ~650ms on my notebook (500,000 instead of the requested 50,000).

    Edit2: with two other tricks, in case the filling time matters:

    • construct the StringBuilder with a sufficient capacity
    • don’t append
      for every line (the code below appends 200 lines every time, for this
      it uses a temp sb1); only possible if every line can have the same
      content. Enjoy.

      long nt = System.nanoTime();
      StringBuilder sb1 = new StringBuilder(400);
      for (int i = 0; i < 200; i++) {
          sb1.append("l").append("\n");
      }
      String strSb1 = sb1.toString();
      
      StringBuilder sb = new StringBuilder(1000000);
      for (int i = 0; i < 2500; i++) {
          sb.append(strSb1);
      }
      
      System.out.print(sb.toString());
      nt = System.nanoTime() - nt;
      System.out.print("\nTime(ms): " + (double)nt / 1000000);
      

    ~500ms in my case.

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

Sidebar

Related Questions

I’m working on the UVa Online Judge problem set archive as a way to
I've been using the UVa Online Judge to solve some programming challenges, and, when
This is my solution for sphere's online judge palin problem . It runs fine
I'm solving sphere's online judge shortest path problem . This bit of code is
I'm trying The Next Palindrome problem from Sphere Online Judge (SPOJ) where I need
I'm solving Sphere's Online Judge Prime Generator using the Sieve of Eratosthenes. My code
I'm developing an Online Judge System,like SGU http://acm.sgu.ru/ I wangt to obtain the accurate
a collegue of mine proposed to me an exercise from an online judge website,
In a lot online judge problems, the format for the input is as follows:
Background I am writing a simple online judge (a code grading system) using PHP

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.