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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:35:21+00:00 2026-05-23T01:35:21+00:00

Possible Duplicate: Performance difference between functions and pattern matching in Mathematica I often find

  • 0

Possible Duplicate:
Performance difference between functions and pattern matching in Mathematica

I often find a heavy use of pure functions in a lot of the answers posted here, and often those solutions are much faster than using named patterns etc. Why is this so? Why are pure functions faster than others? Does it have to do with the mma interpreter having to do less work?

  • 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-23T01:35:21+00:00Added an answer on May 23, 2026 at 1:35 am

    First, let us consider some sample benchmarks:

    In[100]:= f[x_]:=x^2;
    
    In[102]:= Do[#^2&[i],{i,300000}]//Timing
    Out[102]= {0.406,Null}
    
    In[103]:= Do[f[i],{i,300000}]//Timing
    Out[103]= {0.484,Null}
    
    In[104]:= Do[Function[x,x^2][i],{i,300000}]//Timing
    Out[104]= {0.578,Null}
    

    Pure functions are often (much) faster for 2 reasons. First, anonymous pure functions (those defined with the slots – # and &) do not need to resolve name conflicts for variable names. Therefore, they are somewhat faster than pattern-defined ones, where some name conflict resolution takes place. But you see that pure functions with named variables are actually slower, not faster, than pattern-defined ones. I can speculate that this is because they also have to resolve possible conflicts inside their body, while rule-based ones ignore such conflicts. In nay case, speed differences are of the order of 10-20 %.

    Another, and much more dramatic, difference is when they are used in functions such as Map, Scan, Table, etc, because the latter auto-compile on large numerical (packed) lists. But while pure functions can often be compiled, pattern-defined ones fundamentally can not, so this speed gain is inaccessible to them. For example:

    In[117]:= ff[x_] := Sqrt[x];
    
    In[116]:= Map[Sqrt[#] &, N@Range[100000]]; // Timing
    
    Out[116]= {0.015, Null}
    
    In[114]:= Map[ff, N@Range[100000]]; // Timing
    
    Out[114]= {0.094, Null}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is there a performance difference between BETWEEN and IN with MySQL or
Possible Duplicate: Is there a performance difference between i++ and ++i in C++? In
Possible Duplicate: Is there a performance difference between i++ and ++i in C++? Is
Possible Duplicate: Is there a performance difference between i++ and ++i in C++? I
Possible Duplicate: Difference between declaring variables before or in loop? Is there any (or
Possible Duplicate: String concatenation vs String Builder. Performance Any difference (performance and memory usage)
Possible Duplicate: What is the difference between using a struct with two fields and
Possible Duplicate: c difference between malloc and calloc Is there any situation where you
Possible Duplicate: Whats the main difference between int.Parse() and Convert.ToInt32 What is better to
Possible Duplicate: Whats the main difference between int.Parse() and Convert.ToInt32 I would like 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.