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

  • Home
  • SEARCH
  • 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 6840037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:47:30+00:00 2026-05-26T23:47:30+00:00

I have a very simple function in my application that does a lot of

  • 0

I have a very simple function in my application that does a lot of work and occupies the most computation time:

f :: Int -> Array (Int,Int) Int -> [Int]
f x arr = [v | v <- range (l,u), vv <- [g!(x,v)], vv /= 0]
          where ((_,l), (_,u)) = bounds arr

What this does is: extract a row at index x from the array arr and return all the column indices with elements \= 0. So, for instance, given the following matrix with bounds ((0,0),(2,2)):

arr =  [[0, 0, 5], 
        [4, 0, 3],
        [0, 3, 1]]   -- for simplicity in [[a]] notation

the expected output is

f 0 arr == [2] 
f 1 arr == [0,2]
f 2 arr == [1,2]

How do I speed up f and profile with more detail what actually takes most of the computation time in f (list construction, array access, etc)?

Thank you!

  • 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-26T23:47:30+00:00Added an answer on May 26, 2026 at 11:47 pm
    f x arr = [v | v <- range (l,u), vv <- [g!(x,v)], vv /= 0]
    

    I suppose g is a typo, it should be arr.
    Instead of range (l,u) use [l .. u], the compiler may be able to optimise the former as well as the latter, but [l .. u] is more idiomatic (and maybe the compiler can’t optimise the former as well).
    Don’t create a pointless one-element list, you can use the direct test,

    f x arr = [v | v <- [l .. u], arr!(x,v) /= 0]
    

    The compiler may again be able to rewrite the former to the latter, but a) the latter is much clearer, and b) doesn’t risk that the compiler can’t.

    To find out where time is spent, you can insert cost-centre annotations,

    f x arr = [v | v <- {-# SCC "Range" #-} [l .. u], {-# SCC "ZeroTest" #-} (arr!(x,v) /= 0)]
    

    but such annotations disable many optimisations (compiling for profiling always does), so the picture you get from profiling can be skewed and differ from what actually happens in the optimised programme.

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

Sidebar

Related Questions

I have a very simple function that takes a list of comma separated (x,y)
Sorry, a beginner's question: I have a very simple function in my Django application
I'm learning java and I'm trying to make a very simple application that does
I have written a very simple test application that creates a websocket and parses
I have written a very simple function: function editCategory() { $ID = urlencode($_GET['id']); $cname
I have a very simple CLR Function for doing Regex Matching public static SqlBoolean
I have got the following very simple code: function init() { var articleTabs =
i have very simple problem. I need to create model, that represent element of
I have a very simple WPF application in which I am using data binding
I have a very simple bit of script that changes the status of an

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.