From owner-freebsd-bugs@FreeBSD.ORG Tue Apr 21 17:52:23 2015 Return-Path: Delivered-To: freebsd-bugs@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id A40E8E2 for ; Tue, 21 Apr 2015 17:52:23 +0000 (UTC) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2001:1900:2254:206a::16:76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 85A4117B8 for ; Tue, 21 Apr 2015 17:52:23 +0000 (UTC) Received: from bugs.freebsd.org ([127.0.1.118]) by kenobi.freebsd.org (8.14.9/8.14.9) with ESMTP id t3LHqNWe089366 for ; Tue, 21 Apr 2015 17:52:23 GMT (envelope-from bugzilla-noreply@freebsd.org) From: bugzilla-noreply@freebsd.org To: freebsd-bugs@FreeBSD.org Subject: [Bug 199587] libc strncmp() performance Date: Tue, 21 Apr 2015 17:52:23 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: misc X-Bugzilla-Version: 10.1-STABLE X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Many People X-Bugzilla-Who: blackngel1@gmail.com X-Bugzilla-Status: New X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: freebsd-bugs@FreeBSD.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Apr 2015 17:52:23 -0000 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=199587 Bug ID: 199587 Summary: libc strncmp() performance Product: Base System Version: 10.1-STABLE Hardware: Any OS: Any Status: New Severity: Affects Many People Priority: --- Component: misc Assignee: freebsd-bugs@FreeBSD.org Reporter: blackngel1@gmail.com I've been tinkering with the code, out of curiosity, and I've reimplemented strncmp() to check the performance. Here are my results and the benchmark code: 42359800221 cycles -- FreeBSD strncmp() 42113090043 cycles -- FreeBSD strncmp() 42145558182 cycles -- FreeBSD strncmp() 39479522958 cycles -- My Implementation 39447595998 cycles -- My Implementation 39445708599 cycles -- My Implementation My implementation always runs a bit faster and seems more clean. -------------- #include unsigned long long rdtsc(void) { unsigned int low, hi; __asm__ volatile( "rdtsc\n" : "=a"(low), "=d"(hi) : : "memory"); return ((unsigned long long)hi << 32) | (unsigned long long) low; } int strncmpnew(const char *p, const char *q, size_t n) { while (n > 0 && *p && (*p == *q)) n--, p++, q++; return (n == 0) ? 0: ((const unsigned char)*p - (const unsigned char)*q); } int strncmpfbsd(const char *s1, const char *s2, size_t n) { if (n == 0) return (0); do { if (*s1 != *s2++) return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1)); if (*s1++ == '\0') break; } while (--n != 0); return (0); } void test(int (*f)(const char *, const char *, unsigned int n), unsigned int iters, char *msg) { char *str1 = "abcdefghijklmnopqrstuvwxyz0123456789A"; char *str2 = "abcdefghijklmnopqrstuvwxyz0123456789A"; unsigned long foo = 0; unsigned long long int st = rdtsc(); unsigned int i; for (i = 0; i < iters; i++) foo += f(str1, str2, 37); unsigned long long int tot = rdtsc() - st; printf("%20llu cycles -- %s\n", tot, msg); asm volatile( "" :: "g"(foo) : "memory"); } int main(void) { unsigned int iters = 100000000; test(strncmpfbsd, iters, "FreeBSD strncmp()"); test(strncmpfbsd, iters, "FreeBSD strncmp()"); test(strncmpfbsd, iters, "FreeBSD strncmp()"); test(strncmpnew, iters, "My Implementation"); test(strncmpnew, iters, "My Implementation"); test(strncmpnew, iters, "My Implementation"); getchar(); return 0; } -------------- -- You are receiving this mail because: You are the assignee for the bug.