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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:56:00+00:00 2026-06-07T18:56:00+00:00

Given a large array of numbers where: [1, 2, 3, 4 …] => <tag

  • 0

Given a large array of numbers where:

[1, 2, 3, 4 ...] => <tag attrib="1" />
                    <tag attrib="2" />
                    <tag attrib="3" />
                    <tag attrib="4" />
                    ...

Which is more efficient/fast:

a) building them from scratch using Element("name", attributes) and appending them to some root

or

b) fromstring(str) where str is the string representation of those tags in the example

  • 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-07T18:56:03+00:00Added an answer on June 7, 2026 at 6:56 pm

    updated test:

    from xml.etree.ElementTree import Element, XML, tostring
    from timeit import timeit
    
    elist = [e for e in xrange(1000)]
    
    def test_normal():
        eroot = Element('root')
        for e in elist:
            eroot.append(Element("tag", {"attrib" :"%s" %e}))
    
    def test_list():
        eroot = Element('root')
        [eroot.append(Element("tag", {"attrib" :"%s" %e})) for e in elist]
    
    
    print "etree: %.6f" %timeit(test_normal, number=1000)
    print "l-cmp: %.6f" %timeit(test_list, number=1000)
    
    from xml.etree.cElementTree import Element, XML, tostring, fromstring
    #from lxml.etree import Element, XML, tostring
    
    print "ctree: %.6f" %timeit(test_normal, number=1000)
    print "c-cmp: %.6f" %timeit(test_list, number=1000)
    
    def test_string():
        eroot = "<root>"
        tags = ['<tag attrib="%s" />' %e for e in elist]
        eroot += ' '.join(tags) + '</root>'
        tree = fromstring(eroot)
    
    print "strng: %.6f" %timeit(test_string, number=1000)    
    
    
    etree: 13.302093
    l-cmp: 12.276725
    ctree: 5.482961
    c-cmp: 5.692758
    strng: 6.578780
    

    The cElementTree is the fastest version. So i would say: don’t use the string abomination! 😉

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

Sidebar

Related Questions

Given a large unordered array of long random numbers and a target long, what's
Given a large text block from a WYSIWYG like: Lorem ipsum dolor sit amet,
Given a large array how can it be split into smaller arrays with the
You’re given an array of N 64 bit integers. N may be very large.
Given a large string, what is the best way to create an array of
Given a large C project with multiple source files, what is an efficient way
I have established an array and a large textbox which displays the array. Basically,
Given a large legacy project utilizing ASP.NET, javascript, css, etc, technologies, I was wondering
Given a large table (10-100 million rows) what's the best way to add some
Suppose I'm given a large dictionary in flat file with 200 million words and

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.