From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,97482af7429a6a62 X-Google-Attributes: gid103376,public From: graham@compass-da.com (Paul Graham) Subject: Re: Language Efficiency Date: 1995/04/19 Message-ID: #1/1 X-Deja-AN: 101283254 references: organization: Compass Design Automation newsgroups: comp.lang.ada Date: 1995-04-19T00:00:00+00:00 List-Id: Here's an example of language-based inefficiency. Suppose I want to allocate a structure and initialize its fields to some safe value, say 0. In C I would write ptr = malloc(sizeof(struct_type)); memset(ptr, '\0', sizeof(struct_type)); In Ada I would write ptr := new struct_type'(others => 0); or ptr := new struct_type; -- assuming I want the default values The call to memset in the C example can be quite inefficient. memset does not know that ptr is a word-aligned address, so it must fill in the structure using byte moves. I suppose memset could be clever and check for the alignment of ptr and the value of sizeof(struct_type), and jump to an appropriate loop which uses byte moves, word moves, longword moves, etc. The Ada code should know that ptr will be word-aligned. Assuming that it doesn't first create a temporary aggregate, it should be able to fill in ptr's fields efficiently using word moves. I could achieve the same effect in C by writing ptr = malloc(sizeof(struct_type)); ptr->x = 0; ptr->y = 0; ... but this style of initialization breaks easily if I ever add a new field to struct_type. Paul -- Paul Graham graham@compass-da.com Compass Design Automation, Inc. (speaking only for myself) "Cekoslovakyalilastiramadiklarimizdanmissiniz."