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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:04:47+00:00 2026-05-27T21:04:47+00:00

I have been working on a small Plagiarism detection engine which uses Idea from

  • 0

I have been working on a small Plagiarism detection engine which uses Idea from MOSS.
I need a Rolling Hash function, I am inspired from Rabin-Karp Algorithm.

Code I wrote –>

#!/usr/bin/env ruby
#Designing a rolling hash function.
#Inspired from the Rabin-Karp Algorithm

module Myth
  module Hasher

    #Defining a Hash Structure
    #A hash is a integer value + line number where the word for this hash existed in the source file
    Struct.new('Hash',:value,:line_number)

    #For hashing a piece of text we ned two sets of parameters
    #k-->For buildinf units of k grams hashes  
    #q-->Prime which lets calculations stay within range
    def calc_hash(text_to_process,k,q)

      text_length=text_to_process.length
      radix=26

      highorder=(radix**(text_length-1))%q

      #Individual hashes for k-grams
      text_hash=0

      #The entire k-grams hashes list for the document
      text_hash_string=""

      #Preprocessing
      for c in 0...k do
        text_hash=(radix*text_hash+text_to_process[c].ord)%q
      end

      text_hash_string << text_hash.to_s << " "

      loop=text_length-k

      for c in 0..loop do        
        puts text_hash
        text_hash=(radix*(text_hash-text_to_process[c].ord*highorder)+(text_hash[c+k].ord))%q
        text_hash_string << text_hash_string << " "
      end
    end

  end
end

I am running it with values –>
calc_hash(text,5,101) where text is a String input.

The code is very slow. Where am I going wrong?

  • 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-27T21:04:48+00:00Added an answer on May 27, 2026 at 9:04 pm

    Look at Ruby-Prof, a profiler for Ruby. Use gem install ruby-prof to install it.

    Once you have some ideas where the code is lagging, you can use Ruby’s Benchmark to try different algorithms to find the fastest.

    Nose around on StackOverflow and you’ll see lots of places where we’ll use Benchmark to test various methods to see which is the fastest. You’ll also get an idea of different ways to set up the tests.


    For instance, looking at your code, I wasn’t sure whether an append, <<, was better than concatenating using + or using string interpolation. Here’s the code to test that and the results:

    require 'benchmark'
    include Benchmark
    
    n = 1_000_000
    bm(13) do |x|
      x.report("interpolate") { n.times { foo = "foo"; bar = "bar"; "#{foo}#{bar}" } }
      x.report("concatenate") { n.times { foo = "foo"; bar = "bar"; foo + bar      } }
      x.report("append")      { n.times { foo = "foo"; bar = "bar"; foo << bar     } }
    end
    
    ruby test.rb; ruby test.rb
                       user     system      total        real
    interpolate    1.090000   0.000000   1.090000 (  1.093071)
    concatenate    0.860000   0.010000   0.870000 (  0.865982)
    append         0.750000   0.000000   0.750000 (  0.753016)
                       user     system      total        real
    interpolate    1.080000   0.000000   1.080000 (  1.085537)
    concatenate    0.870000   0.000000   0.870000 (  0.864697)
    append         0.750000   0.000000   0.750000 (  0.750866)
    

    I was wondering about the effects of using fixed versus variables when appending strings based on @Myth17’s comment below:

    require 'benchmark'
    include Benchmark
    
    n = 1_000_000
    bm(13) do |x|
      x.report("interpolate") { n.times { foo = "foo"; bar = "bar"; "#{foo}#{bar}" } }
      x.report("concatenate") { n.times { foo = "foo"; bar = "bar"; foo + bar      } }
      x.report("append")      { n.times { foo = "foo"; bar = "bar"; foo << bar     } }
      x.report("append2")     { n.times { foo = "foo"; bar = "bar"; "foo" << bar   } }
      x.report("append3")     { n.times { foo = "foo"; bar = "bar"; "foo" << "bar" } }
    end
    

    Resulting in:

    ruby test.rb;ruby test.rb
    
                       user     system      total        real
    interpolate    1.330000   0.000000   1.330000 (  1.326833)
    concatenate    1.080000   0.000000   1.080000 (  1.084989)
    append         0.940000   0.010000   0.950000 (  0.937635)
    append2        1.160000   0.000000   1.160000 (  1.165974)
    append3        1.400000   0.000000   1.400000 (  1.397616)
    
                       user     system      total        real
    interpolate    1.320000   0.000000   1.320000 (  1.325286)
    concatenate    1.100000   0.000000   1.100000 (  1.090585)
    append         0.930000   0.000000   0.930000 (  0.936956)
    append2        1.160000   0.000000   1.160000 (  1.157424)
    append3        1.390000   0.000000   1.390000 (  1.392742)
    

    The values are different than my previous test because the code is being run on my laptop.

    Appending two variables is faster than when a fixed string is involved because there is overhead; Ruby has to create an intermediate variable and then append to it.

    The big lesson here is we can make a more informed decision when we’re writing code because we know what runs faster. At the same time, the differences are not very big, since most code isn’t running 1,000,000 loops. Your mileage might vary.

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

Sidebar

Related Questions

i have been working on a small demo and i wrote a function which
I have been working with a small project recently which involves Struts 2 and
I have been working on a project which uses Tiger's SQLite3 library (which if
I have been working now for few days on a small C program which
I have been working on a small php app (400K total). But in the
I have been working with a string[] array in C# that gets returned from
I have been working on some legacy C++ code that uses variable length structures
I've been working on a small blog form but have been having trouble with
Ok so, I have been working on a small webapp in sinatra. After taking
So I have been working on getting a small (> 1 MB) video to

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.