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

  • Home
  • SEARCH
  • 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 8142871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:56:29+00:00 2026-06-06T12:56:29+00:00

First piece of code: // code is a private global variable for the class

  • 0

First piece of code:

// code is a private "global variable" for the class
// SourceCodeBuilder is a class that uses StringBuilder()
// basically it is based on String(s), formatted and with many appends depending on the "loc()" calls (see below)
private SourceCodeBuilder code = new SourceCodeBuilder();

[...]

    // create "file.txt" and call algorithm
    fileOut = new FileWriter("file.txt");

    for (int i=0; i<x; i++) {
        algorithm();
    }

Where algorithm() is a method like this:

private void algorithm () {
    for (int i=0; i<y; i++) {
        code.loc("some text");
        code.loc("other text");
        ...
    }

    // after "building" the code value I wrote it on the file
    fileOut.write(code.toString());
    fileOut.flush();
    code.free(); // this call "empties" the code variable (so the next time algorithm() is called it has the code var sets to "" - it frees a lot of memory)
                 // basically it calls "setLength(0)" method of StringBuilder
}

When I do all of this on large text files it takes about 4500ms to execute and less than 60MB of memory.

Then I tried to use this other code.
Second piece of code:

private SourceCodeBuilder code = new SourceCodeBuilder();

[...]

    // create "file.txt" and call algorithm
    fileOut = new FileWriter("file.txt");

    for (int i=0; i<x; i++) {
        algorithm();
    }

    fileOut.write(code.toString());
    fileOut.flush();
    fileOut.close();

Where this time algorithm() is a method like this:

private void algorithm () {
    for (int i=0; i<y; i++) {
        code.loc("some text");
        code.loc("other text");
        ...
    }
}

It takes more than 250MB of memory (and it’s OK because I don’t call the “free()” method on the code variable, so it’s a “continuos” append on the same variable), but surprisingly it takes more than 5300ms to execute.
That’s about 16% slower than the first code, and I can’t explain to myself why.

In the first code I write small pieces of text multiple times on “file.txt”. In the second code I write a big piece of text, but only one time, on “file.txt”, and using more memory. With the second code I was expecting more memory consumption, but not even more CPU consumption (just because there are more I/O operations).

Conclusion: the first piece of code is faster than the second one, even if the first one does more I/O operations than the second one. Why? Am I missing something?

  • 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-06-06T12:56:30+00:00Added an answer on June 6, 2026 at 12:56 pm

    When you are slowly filling a large memory buffer, the time required for that grows non-linearly, because you need to re-allocate the buffer multiple times, each time copying the entire content to a new location in memory. This takes time, especially when the buffer is 200MB+. If you preallocate the buffer, your process may go faster.

    However, all the above is just my guess. You should profile your application to see where the additional time really goes.

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

Sidebar

Related Questions

I have such html piece of code: <ul class=myClass> <li><a href=url1.html>first link</a></li> <li><a href=url2.html>second
my first question here! I have a problem with a piece of code that
Here is a piece of code: private class myClass { public static void Main()
Firstly sorry for the long piece of code pasted below. This is my first
I have a piece of code that creates an SQL Server Express 2008 in
I have written a piece of code : public class Child{ int y ;
I've used this piece of code to put an image in a variable. What
In a new piece of code I have several different classes that refer to
I have written a PHP script based on a piece of code I've found
I have this piece of LINQ code private static void Original() { using (var

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.