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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:52:23+00:00 2026-06-14T02:52:23+00:00

Problem can be found here: http://rosalind.info/problems/subs/ The question I have has to do with

  • 0

Problem can be found here:
http://rosalind.info/problems/subs/

The question I have has to do with the performance of the two solutions provided below.

1.

  def indexOfAppearances(strand: String, subStrand: String): List[Int] = {
    def help0(x: String, y: String, accu: List[Int]): List[Int] =
      (x contains y) match {
        case true => {
          val index = (x indexOfSlice y) /* index where substring appears */
          val adjust = strand.length - x.length
          /* adjustment since the x has all the previous
               * nucleotides removed.
               */

          val newX = x.drop(index + 1).mkString
           /* here's the drop of index + 1 elements */
          help0(newX, y, (index + adjust) :: accu) /* tail recursive call */
        }
        case false => accu
      }

     help0(strand, subStrand, List()).reverse.toList.map(x => x + 1)
         //indexes are not from 0 but from 1
  }

2.

  val s = "ACGTACGTACGTACGT"
  val t = "GTA"

  val combs = s.sliding(t.length).zipWithIndex
  val locations = combs.collect { case (sub, i) if (sub == t) => i + 1 }
  println(locations.mkString(" "))

The second solution is pretty, functional and short.

First solution is a bit large but it’s still functional. I could have left out the val’s and just work with the values to make it shorter but that wasn’t my goal.

After seeing the 2nd solution I was quite disappointed by mine due to the length of code. Checked the scala library to see why the 2nd solution works and then re-implemented it myself.
Thought about checking the performance of both solutions and made a huge 30 million DNA strand.

Surprised!

Performance:

First number is the DNA length, and the next two numbers present the execution time of 1st and 2nd solution(in ms).

11,226,096 – 4921 – 14503

33,678,288 – 6448 – 35150

Why is the performance so much different?

I’ve tried checking the scala library but couldn’t find a thing which explains this behaviour.

I assumed that the first solution is creating many objects thus consuming more memory and taking a lot of time to do that but it seems that for some reason it works much faster.
I doubt it’s the tail recursion and I doubt that zipWithIndex takes a lot of time. Iterators are just iterators?

Thanks!

  • 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-14T02:52:25+00:00Added an answer on June 14, 2026 at 2:52 am

    sliding isn’t efficient with strings. It breaks the string down into characters, boxes them, then reassembles them into a string.

    The fastest way to do this which is still not incredibly hard is with the regionMatches method on String. (Faster yet with DNA is to convert everything into bytes, faster yet is to convert it into 2-bit nibbles and pack into int arrays.)

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

Sidebar

Related Questions

My problem is in my navigation bar, which can be found here: http://grupocoral.netai.net/ There
The problem can be found here : http://projecteuler.net/problem=11 Yesterday I saw project Euler and
Code can be found here: http://www.myhorizon.ca/client_central/sortable_test.php Hello folks of Stackoverflow, I have a list
i have a little problem, i'm searching, but i can't found what i need,
I have looked around here to see if somebody has asked this question before
I'm currently using the jquery plugin tablesorter, found here: http://tablesorter.com/docs/#Download My problem is, when
I added plugin $.fn.dataTableExt.oApi.fnGetColumnData Can be found here: http://datatables.net/plug-ins/api After I executed $(document).ready(function ()
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
Possible Duplicate: Can main function call itself in C++? I found this problem very
I've searched all over and have found possible solutions but they don't quite work

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.