From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3251 invoked by alias); 13 Jan 2018 08:35:47 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 3043 invoked by uid 89); 13 Jan 2018 08:35:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL,BAYES_00,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=no version=3.3.2 spammy=buying, credit, pay X-HELO: mail-io0-f172.google.com Received: from mail-io0-f172.google.com (HELO mail-io0-f172.google.com) (209.85.223.172) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 13 Jan 2018 08:35:12 +0000 Received: by mail-io0-f172.google.com with SMTP id c17so8333152iod.1 for ; Sat, 13 Jan 2018 00:35:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=3p99X2qAx/J0qOqULKMNdYgdWWCQQPdt+eMT9G8hbFc=; b=iYjzIIrp0X82LJ7UW16xGs0RUITSYwEzE8OokxVusZ9T8tpQEVHOCH3fAQl2U35z2r ggXSrVpFDIgVqwWiGj1rgjA+A52Tc4V1/NqioRHnB+tjyoXhGa4B+f3VvAqrix54C4mw 66cOxcL413N94ysCvWAbtwI6lfrlqFMSfRGXjoAao+y8+mnveh83C2d7tHdIuchXDW7X /c0Y4w0UYf7sHQ9FimI+Cawbr8CcA22t3Or2lF4HDIJW0HnQKxhUhCgDJtS50cq5G7RA HT/Kao7ltEUlGW9SQFddAa9UtwPVMKXKzSiArBNdVzaTPcPB4TMJjxJ2G8nXolrZRAGu 95LA== X-Gm-Message-State: AKGB3mKjUfx/QmFtCJ9to0BAqkAPvCgvhrxGNqdMGZQ8J4KHhxKoP0wl Em+1gX/bnEJMLYv1osuyqJrEGUkllPWbZs5chDY= X-Google-Smtp-Source: ACJfBov1fNnozKs6Io3IZPpEZ1OGqgPdfzvEEZRSpjm8+4ayx5QCRsxEsps0x9/uoRihIHgbBaoBrgvaLx/eP1hu41s= X-Received: by 10.107.166.18 with SMTP id p18mr28792665ioe.158.1515832510246; Sat, 13 Jan 2018 00:35:10 -0800 (PST) MIME-Version: 1.0 Received: by 10.79.147.218 with HTTP; Sat, 13 Jan 2018 00:35:09 -0800 (PST) In-Reply-To: <46515148-9f8e-6eae-69f9-9bf20921097a@t-online.de> References: <46515148-9f8e-6eae-69f9-9bf20921097a@t-online.de> From: Lee Date: Sat, 13 Jan 2018 08:35:00 -0000 Message-ID: Subject: Re: calloc speed difference To: cygwin@cygwin.com Content-Type: text/plain; charset="UTF-8" X-IsSubscribed: yes X-SW-Source: 2018-01/txt/msg00129.txt.bz2 On 1/12/18, Christian Franke wrote: > Lee wrote: >> Why is the cygwin gcc calloc so much slower than the >> i686-w64-mingw32-gcc calloc? >> 1:12 vs 0:11 >> >> $cat calloc-test.c >> #include >> #include >> #define ALLOCATION_SIZE (100 * 1024 * 1024) >> int main (int argc, char *argv[]) { >> for (int i = 0; i < 10000; i++) { >> void *temp = calloc(ALLOCATION_SIZE, 1); >> if ( temp == NULL ) { >> printf("drat! calloc returned NULL\n"); >> return 1; >> } >> free(temp); >> } >> return 0; >> } >> > > Could reproduce the difference on an older i7-2600K machine: > > Cygwin: ~20s > MinGW: ~4s > > Timing [cm]alloc() calls without actually using the allocated memory > might produce misleading results due to lazy page allocation and/or > zero-filling. > > MinGW binaries use calloc() from msvcrt.dll. This calloc() does not call > malloc() and then memset(). It directly calls: > > mem = HeapAlloc(_crtheap, HEAP_ZERO_MEMORY, size); > > which possibly only reserves allocate-and-zero-fill-on-demand pages for > later. Which seems like it could be viewed as a feature? Sort of like buying on credit - you don't pay for it all up front, just pay a bit each time you reference another zero fill on demand page. > Cygwin's calloc() is different. > > This variant of the above code adds one write access to each 4KiB page > (guarded by "volatile" to prevent dead assignment optimization): > > #include > #include > #define ALLOCATION_SIZE (100 * 1024 * 1024) > int main (int argc, char *argv[]) { > for (int i = 0; i < 1000; i++) { > void *temp = calloc(ALLOCATION_SIZE, 1); > if ( temp == NULL ) { > printf("drat! calloc returned NULL\n"); > return 1; > } > for (int j = 0; j < ALLOCATION_SIZE; j += 4096) > ((volatile char *)temp)[j] = (char)i; > free(temp); > } > return 0; > } > > Results: > > Cygwin: ~310s > MinGW: ~210s Wow! Really nice explanation & example - Thank you. Lee -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple