Timing of looping operators

• To: mathgroup at smc.vnet.net
• Subject: [mg62416] Timing of looping operators
• From: dh <dh at metrohm.ch>
• Date: Wed, 23 Nov 2005 04:31:55 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Hello,
The Mathematica gospel tells you that you should NOT use loops because
it is inefficient.
Well consider the following examples and their times:

n=10^6;
d=Table[i,{i,n}];
fun[x_]:=x;

a)  Timing[fun & /@ d;]       needs 0.8 sec
b)  Timing[Scan[fun, d]]      needs 1 second
c)  Timing[Do[f[i], {i, n}];] needs 0.7 sec

a) applies the function and creates a new list. b) does not create a new
list -- but it is slower! And finally c) the loop is fastest!!!

If you change the function to: f[x_]:=x^2, the times are even more striking:

0.8, 2.4, 0.7
it seems like in a and c the function evaluation takes negliable time
compared to the loop construct, but not so in b.

has anybody an explanation???

Daniel

```

• Prev by Date: Re: How to View Mathematica and Hardcopy Books
• Next by Date: permutations
• Previous by thread: MATHwire: Information for Mathematica Users
• Next by thread: Re: Timing of looping operators
{"ft_lang_label":"__label__en","ft_lang_prob":0.7926759,"math_prob":0.7152681,"size":1203,"snap":"2019-51-2020-05","text_gpt3_token_len":373,"char_repetition_ratio":0.15679733,"word_repetition_ratio":0.07772021,"special_character_ratio":0.30507067,"punctuation_ratio":0.20938627,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97525537,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-25T06:36:39Z\",\"WARC-Record-ID\":\"<urn:uuid:d66b8c84-e6f6-4c9a-84e3-c15b115720f8>\",\"Content-Length\":\"44995\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4f8e9e69-d30a-4488-b4fd-494c60158700>\",\"WARC-Concurrent-To\":\"<urn:uuid:fb63a961-dbc7-475a-bc18-9021d6bc85e4>\",\"WARC-IP-Address\":\"140.177.205.73\",\"WARC-Target-URI\":\"http://forums.wolfram.com/mathgroup/archive/2005/Nov/msg00610.html\",\"WARC-Payload-Digest\":\"sha1:PPD7XMNKTAUVA3AL3CJDMJT573TWCSCR\",\"WARC-Block-Digest\":\"sha1:JO5ZHPNHCL2E3R5MLQFPKGYQIQAT7BLV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251669967.70_warc_CC-MAIN-20200125041318-20200125070318-00191.warc.gz\"}"}