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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:37:22+00:00 2026-05-18T05:37:22+00:00

Assume I have a mathematical expression in a tree form in OCaml. It’s represented

  • 0

Assume I have a mathematical expression in a “tree” form in OCaml. It’s represented as an algebraic type like this:

type expr = 
   Number of int
  |Plus of expr*expr

Well, this is a very simplified definition, but it’s enough to describe the problem.

I want to convert it to a string in a reverse polish notation, so that Plus (Number i, Number j) becomes (+ i j). A straightforward implementation would be

let rec convert = function
   Number i -> string_of_int i
  |Plus (a,b) -> (let s = convert a in let p = convert b in "(+"^s^" "^p^")")

But the thing is that it’s incredibly slow on some input (that have a big tree depth). For example, this input works 5 seconds on my machine:

let rec make_pain so_far = function
  0 -> so_far |i -> make_pain (Plus (Number 1,so_far)) (i-1)

let pain = make_pain (Number 1) 20000

let converted = convert pain

It seems that string concatenation x^y, where y is a long string, is the performance problem. Indeed, if I replace the "(+"^s^" "^p^")" expression with mere s^p, it becomes much faster.

Using printf instead of concatenation doesn’t make it any faster. Converting to C might help, but isn’t there an OCaml solution?

  • 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-18T05:37:22+00:00Added an answer on May 18, 2026 at 5:37 am

    Use a string Buffer.

    ^ is defined as,

    let (^) s1 s2 =
      let l1 = string_length s1 and l2 = string_length s2 in
      let s = string_create (l1 + l2) in
      string_blit s1 0 s 0 l1;
      string_blit s2 0 s l1 l2;
      s
    

    What you are doing is creating a new string each time and copying the old values in. Pretty much standard in any language where strings are represented as character arrays. The hangup happens because you are doing this FOUR times for each node (there isn’t an optimization for multiple ^ calls)! As for a buffer, it will create a giant string and continually fill it in as managed by the data structure,

     type t =
       {mutable buffer : string;
        mutable position : int;
        mutable length : int;
        initial_buffer : string}
    

    Even if you decide to create the initial buffer size to 1, the resize function will adjust the size in a way that will limit the number of re-allocations. For example, the add_string function will increase the size of the array by len*2^(n+p-len), where n is the length of the new string, p is the position and len is the length of the original buffer –only if the buffer cannot support the string, of course. So, the size of the buffer grows exponentially and there will be few re-allocations as things progress. Of course, it’s best to set the initial buffer to something reasonable, but it isn’t necessary.

    The new convert function wouldn’t look much more verbose:

    let rec convert buf ex =
      let addb = Buffer.add_string buf in
      match ex with
       Number i -> addb (string_of_int i)
      |Plus (a,b) -> (addb "(+ "; convert buf a; addb " "; convert buf b; addb ")")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker
Lets assume we have this xml: <?xml version=1.0 encoding=UTF-8?> <tns:RegistryResponse status=urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Failure xmlns:tns=urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0 xmlns:rim=urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0> <tns:RegistryErrorList
Assume I have a class foo, and wish to use a std::map to store
Assume I have created a compiled re: x = re.compile('^\d+$') Is there a way
Assume you have some objects which have several fields they can be compared by:
Assume I have an ASP.NET MVC app that's not doing anything too fancy (no
Assume I have 10 Methods and 10 Properties. Is there a way to add
Assume you have five products, and all of them use one or more of

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.