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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:55:46+00:00 2026-05-26T00:55:46+00:00

I have a large array of hashes, about 0.5Gb, stored in memory, and I

  • 0

I have a large array of hashes, about 0.5Gb, stored in memory, and I need to remove some elements from it, about 10% spread around the whole array.

What is likelier to work best, doing a grep, or identifying the elements that need deleting, and splice-ing them out?

Thanks,

Simone

  • 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-26T00:55:47+00:00Added an answer on May 26, 2026 at 12:55 am

    splice may go to O(n^2) under conditions you describe (as it moves array content around), and grep/slice would allocate O(n) extra memory (probably, much less than 500GB, but still…).

    There is a linear solution with no extra memory though, but looks more like C than like Perl:

    sub inplace_grep {
        my ($code, $array) = @_; 
        # move elements backwards
        for (my ($to, $from)=(0,0); $from < @$array; $from++) {
            $code->($array->[$from]) or next;
            $array->[$to++] = $array->[$from];
        };
        # remove tail 
        splice @$array, $to; 
    };
    

    Update: on grep memory usage — you can do a quick test for extra memory allocation by using large amounts of data and looking for brk syscall. On my system (linux, perl 5.10) it does.

    strace -e trace=brk perl -MTime::HiRes -wle \
    'print "start ".time; my @array = 1..10**7; print "alloc ".time;
    @array = grep { $_ %2 } @array; print "grep ".time'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large NumPy array: a = np.array . From this array
I have a PHP file that contains a large array (about 90KB). I'm considering
I have a large array (say 512K elements), GPU resident, where only a small
I have a large nested array that I'm generating from parsing a CSV file
I have a large array in RAM and want to read data from it
I have a large array of numerical data that I need to sort, insert,
I have a large array. I have some Java code for identifying indices for
I have a large array of vertices, some of them are edges, some are
I have a large array with around 20k objects in it. Each object has
I have a large array at hand (2300 elements, 280 KB in total, 180

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.