2

I have 100,000 strings of length 10, which needs to be split in 9 ways as shown below

10 # the whole string
5 x 5 # 2 strings of length 5 each
4 x 6 # and so on...
6 x 4
3 x 3 x 4
3 x 4 x 3
4 x 3 x 3
3 x 7
7 x 3

So for example the string WGWJAWMPPJ would turn into

WGWJAWMPPJ
WGWJA WMPPJ
WGWJ AWMPPJ
WGWJAW MPPJ
WGW JAW MPPJ
WGW JAWM PPJ
WGWJ AWM PPJ
WGW JAWMPPJ
WGWJAWM PPJ

The code I wrote is below

def breakdown(str)
  [
    [str],
    [str[0..4], str[5..9]],
    [str[0..3], str[4..9]],
    [str[0..5], str[6..9]],
    [str[0..2], str[3..5], str[6..9]],
    [str[0..2], str[3..6], str[7..9]],
    [str[0..3], str[4..6], str[7..9]],
    [str[0..2], str[3..9]],
    [str[0..6], str[7..9]]
  ]
end

Is there any way to improve the performance of the above method?

5
  • 1
    My money is on "no". I'm curious: why do you want to do that and why do you need better performance? I'd think your code would split 100,000 strings in the blink of an eye. Commented Sep 8, 2019 at 6:01
  • You'll end up with a lot of substring creation and copying. You might want to try this on an implementation like Rubinius, which AFAIK uses ropes instead of strings to implement the String class. The operations you are performing, are what ropes are very good at. Commented Sep 8, 2019 at 6:31
  • do you need to return an array always or just a string output as you've shown in your sample output? Commented Sep 8, 2019 at 8:15
  • @CarySwoveland I was just curious whether there is a better way to do this Commented Sep 9, 2019 at 8:54
  • @lacostenycoder Yes, I need to return array Commented Sep 9, 2019 at 8:55

2 Answers 2

2

I suppose you could gain a bit of speed storing repetitive slices in variables:

def breakdown(str)
  s_0_2 = str[0..2]
  s_0_3 = str[0..3]
  s_6_9 = str[6..9]
  s_7_9 = str[7..9]
  [
    [str],
    [str[0..4], str[5..9]],
    [s_0_3, str[4..9]],
    [str[0..5], s_6_9],
    [s_0_2, str[3..5], s_6_9],
    [s_0_2, str[3..6], s_7_9],
    [s_0_3, str[4..6], s_7_9],
    [s_0_2, str[3..9]],
    [str[0..6], s_7_9]
  ]
end
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you are correct. My benchmark test shows about 25% faster .
2

What is your definition of "fast" ? Did you bench mark it?

require 'benchmark'
require 'SecureRandom'

arr = []

100_000.times { arr << SecureRandom.hex(5)}

def breakdown(str)
  [
    [str],
    [str[0..4], str[5..9]],
    [str[0..3], str[4..9]],
    [str[0..5], str[6..9]],
    [str[0..2], str[3..5], str[6..9]],
    [str[0..2], str[3..6], str[7..9]],
    [str[0..3], str[4..6], str[7..9]],
    [str[0..2], str[3..9]],
    [str[0..6], str[7..9]]
  ]
end

Benchmark.bm do |x|
  x.report { arr.each{|a| breakdown a} }
end

       user     system      total        real
a  0.337387   0.001583   0.338970 (  0.341292)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.