octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Is there an easy way to speed cellstr()?


From: Daniel J Sebald
Subject: Is there an easy way to speed cellstr()?
Date: Sun, 29 Jul 2012 13:14:14 -0500
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111108 Fedora/3.1.16-1.fc14 Thunderbird/3.1.16

John,

Here's a question for you, regarding trying to speed up cellstr(). Seeing as you are fluent in advanced C++ constructs, thinking about this might give you an idea that I can't fathom other than in concept form.

Basically, the current setup is pretty straightforward: pass a string list into the Cell class (inheriting Array) as follows:

Cell::Cell (const std::list<std::string>& lst)
  : Array<octave_value> ()
{
  size_t n = lst.size ();

  if (n > 0)
    {
      resize (dim_vector (n, 1));

      octave_idx_type i = 0;

      for (std::list<std::string>::const_iterator it = lst.begin ();
           it != lst.end (); it++)
        {
          elem(i++,0) = *it;
        }
    }
}

I suspect that the slowness of cellstr() comes from that

elem(i++,0) = *it;

assignment, which I assume translates to a memory allocation on an element-by-element basis.

Could there be a way to define a constructor that has an initializing element passed on to the inherited class [like Array(std::list< T >)] and some smart way of constructing all the elements with a single memory alloc? Say for example "new (list-length)". Then instead of

      for (std::list<std::string>::const_iterator it = lst.begin ();
           it != lst.end (); it++)
        {
          elem(i++,0) = *it;
        }

element-by-element assignment inside the constructor one could access the characters of the elements via some strange C++ construct inside cellstr after constructor is called? That way one could overwrite the default characters with the new strings since they are all the same length. E.g., (these are only pseudo commands):

          string_vector s = args(0).all_strings ();
          retval = Cell (s, true);
          int common_string_length = s(0).length ();
          for (<indextype> i = 0; i < s.length(); i++)
             {
                retval(i).insert(s(i).c_str(), common_string_length);
             }

Dan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]