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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:10:05+00:00 2026-05-27T07:10:05+00:00

In an attempt to optimise my bash script I loaded my file into an

  • 0

In an attempt to optimise my bash script I loaded my file into an array and tried to grep from there, and I notice that this grep from memory is much, much slower than a standard grep from file, even taking into account the fact that the disk I/O is taking out of the equation.

1) OK so I have a large file (about 3000 lines) with name=value pairs, this is my “cache”. I load it into an array from file (straight-forward enough)

# load to array
l_i_array_index=1
while read line
  do
  g_a_cache[$l_i_array_index]=$line
  let "l_i_array_index += 1"
  done < $g_f_cache

2) Then I run a little benchmark for the search performance:

time for i in `seq 1 100`
  do
  for l_i_array_index in `seq 1 ${#g_a_cache[@]}`
    do
      echo ${g_a_cache[$l_i_array_index]}
    done | grep -c $l_s_search_string > /dev/null
  done

real    0m14.387s
user    0m13.846s
sys     0m1.781s

3) The same, but directly from disk file:

time for i in `seq 1 100`
  do
  grep -c $l_s_search_string $g_f_cache > /dev/null
  done
real    0m0.347s
user    0m0.161s
sys     0m0.136s

So performance is 13 to 40 times worse, when it should be better.

My question is: 1) what is the reason for this strange behaviour 2) is this solvable within bash or should I bite the bullet and finally redo it in Python

P.S. The test is done on Mac (bash v4), in Cygwin, time per ONE search is over a second with normal grep (which is faster), and over 10s with the array method. The script is close to unusable..

  • 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-27T07:10:05+00:00Added an answer on May 27, 2026 at 7:10 am

    The grep program has been heavily optimized over many, many years by top experts in the field of searching and algorithm design. You’re simply not going to beat it with a shell script. That’s an absurd notion.

    I honestly can’t imagine why you would expect to be anywhere near as fast as grep. Perhaps you’re thinking that all of greps disk I/O actually requires doing something to the actual physical disk. But that’s not so. Every modern operating system has a disk cache, and the file will be in the cache after the first time it’s read, which takes a tiny fraction of a second.

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

Sidebar

Related Questions

Clone attempt over ssh: Cloning into repo... Password: bash: git-upload-pack: command not found fatal:
First attempt to use this cool site - after searching for 2 hours: So
Has there been any attempt and creating a formalized method for organizing CSS code?
This is an attempt to rephrase a question I asked earlier. I'd like to
Why does this attempt at creating a list of curried functions not work? def
my attempt to init an array with a number of bool values using: [myArray
Is there any attempt to re-make something like Vimeo's couch mode http://vimeo.com/couchmode in jQuery?
This is an attempt to rephrase my previous question as a result of the
I am performing a scattered read of 8-bit data from a file (De-Interleaving a
In attempt to learn Hadoop, I am practicing unsolved programming questions from the book

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.