# Count elements in the real world in constant time by weighing them

I suppose that counting n elements should be linear time, right? It takes double time to count double number of elements. But in the real world, it is faster and O(1) to weigh elements and find out the total weight so in the real world you can count n elements O(1) just by putting all the elements on a scale and dividing by the weight of one elements.

Is my reasoning correct that physically it is possible to speed up a calculation by this trick, or am I mistaken?

• If counting things in a collection is important, it's usually easier to keep track of the count as the collection is being built or maintained. – Pseudonym Jul 19 '16 at 0:34

Weighing elements works faster than counting them, because the scale is a fully parallel computing device. It sums the weighs of all elements in constant time. Of course you need an infinitely large scale if you want to talk about asymptotic runtimes. In practice your scale can only weigh a constant number of items at once, so your speedup is only a constant factor.

A related algorithm is Spaghetti sort.

You make an accurate but rather basic observation: complexities depend on the model of computation you assume. In your informally described model "the real world", some things may indeed work differently than they do for TMs. Be wary of making wrong assumptions, though.

Here, you change the problem. You move from "counting" to "computing total weight". These are only the same if all items have the same weight.

Keep in mind that you have to put the items on the scale first. Assuming you can only put constantly many things on the scale at a time, this makes your method take linear time as well. And that's assuming a scale with unlimited capacity, which you probably don't have. Simply counting items where they lie is probably faster and more energy efficient.

You're mistaken: it takes more than constant time to divide the total mass by the weight of a single object to compute the count.

Physical considerations, such as whether your scales can measure arbitrary weights or whether you can put arbitrary numbers of things on the scales at the same time (as mentioned by adrianN and Raphael) could maybe be swept under the carpet by just defining the computational model to allow that. But assuming that you can divide two arbitrarily long numbers in constant time seems to be a much bigger deal.

• And still, the uniform cost model is the predominant one in many places. – Raphael Jul 19 '16 at 17:06
{"ft_lang_label":"__label__en","ft_lang_prob":0.91614455,"math_prob":0.9598929,"size":467,"snap":"2019-26-2019-30","text_gpt3_token_len":102,"char_repetition_ratio":0.14686824,"word_repetition_ratio":0.0,"special_character_ratio":0.21627408,"punctuation_ratio":0.071428575,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98234206,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-07-24T06:34:15Z\",\"WARC-Record-ID\":\"<urn:uuid:47a35f30-9341-45fc-bd10-b7c23aab1ead>\",\"Content-Length\":\"141975\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ec2da8c5-13b2-4a56-95f7-ed836a447050>\",\"WARC-Concurrent-To\":\"<urn:uuid:1b877a5b-a014-4cd3-9fe8-febb77d9494c>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/60734/count-elements-in-the-real-world-in-constant-time-by-weighing-them\",\"WARC-Payload-Digest\":\"sha1:S6XCNPXLYDWNWL2AAZQ2SRXTSEN56MCI\",\"WARC-Block-Digest\":\"sha1:DPG4DKJGO72D6XUGOFE5IH3Z6MPW37TG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-30/CC-MAIN-2019-30_segments_1563195531106.93_warc_CC-MAIN-20190724061728-20190724083728-00256.warc.gz\"}"}