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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:09:06+00:00 2026-05-14T01:09:06+00:00

I’m trying to create a large XML tree in R. Here’s a simplified version

  • 0

I’m trying to create a large XML tree in R. Here’s a simplified version of the code:

library(XML)
N = 100000#In practice is larger  10^8/ 10^9
seq = newXMLNode("sequence")
pars = as.character(1:N)
for(i in 1:N)
    newXMLNode("Parameter", parent=seq, attrs=c(id=pars[i]))

When N is about N^6 this takes about a minute, N^7 takes about forty minutes. Is there anyway to speed this up?

Using the paste command:

par_tmp = paste('<Parameter id="', pars, '"/>', sep="")

takes less than a second.

  • 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-14T01:09:06+00:00Added an answer on May 14, 2026 at 1:09 am

    I would recommend profiling the function using Rprof or the profr package. This will show you where your bottleneck is, and you then you can think about ways to either optimize the function or change the way that you’re using it.

    Your paste example would be much faster in part because it’s vectorized. For a more fair comparison, you can see the difference there by looping over paste as you are currently doing with newXMLNode and see the difference in timing.

    Edit:

    Here is the output from profiling your loop with profr.

    library(profr)
    xml.prof <- profr(for(i in 1:N) 
        newXMLNode("Parameter", parent=seq, attrs=c(id=pars[i])))
    plot(xml.prof)
    

    There is nothing especially obvious in here about places that you can improve this. I see that it spends a reasonable amount of time in the %in% function, so improving that would reduce the overall time somewhat (although you still need to iterate over this repeatedly, so it won’t make a huge difference). The best solution would be to rewrite newXMLNode as a vectorized function so you can skip the for loop entirely.
    alt text

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

Sidebar

Related Questions

No related questions found

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.