mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: 张飞 <zhangfei@nj.iscas.ac.cn>
Cc: musl@lists.openwall.com
Subject: Re: Re: [musl] memset_riscv64
Date: Wed, 19 Apr 2023 11:02:10 +0200	[thread overview]
Message-ID: <20230419090210.GR3630668@port70.net> (raw)
In-Reply-To: <658c32ae.2348c.187980096c9.Coremail.zhangfei@nj.iscas.ac.cn>

* 张飞 <zhangfei@nj.iscas.ac.cn> [2023-04-19 13:33:08 +0800]:
> --------------------------------------------------------------------------------
> length(byte)  C language implementation(s)   Basic instruction implementation(s)
> --------------------------------------------------------------------------------	
> 4	          0.00000352	                    0.000004001	
> 8	          0.000004001	                    0.000005441	
> 16	          0.000006241	                    0.00000464	
> 32	          0.00000752	                    0.00000448	
> 64	          0.000008481	                    0.000005281	
> 128	          0.000009281	                    0.000005921	
> 256	          0.000011201	                    0.000007041	

i don't think these numbers can be trusted.

> #include <stdio.h>
> #include <sys/mman.h>
> #include <string.h>
> #include <stdlib.h>
> #include <time.h>
> 
> #define DATA_SIZE 5*1024*1024
> #define MAX_LEN 1*1024*1024
> #define OFFSET 0
> #define LOOP_TIMES 100
> int main(){
>    char *str1,*src1;
>    str1 = (char *)mmap(NULL, DATA_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
> 
>    printf("function test start\n");
>    
>    src1 = str1+OFFSET;
>    struct timespec tv0,tv;
>    for(int len=2; len<=MAX_LEN; len*=2){
>       clock_gettime(CLOCK_REALTIME, &tv0);
>       for(int k=0; k<LOOP_TIMES; k++){
>           memset(src1, 'a', len);
>       }
>       clock_gettime(CLOCK_REALTIME, &tv);
>       tv.tv_sec -= tv0.tv_sec;
>       if ((tv.tv_nsec -= tv0.tv_nsec) < 0) {
> 	      tv.tv_nsec += 1000000000;
> 	      tv.tv_sec--;
>       }
>       printf("len: %d  time: %ld.%.9ld\n",len, (long)tv.tv_sec, (long)tv.tv_nsec);


this repeatedly calls memset with exact same len, alignment and value.
so it favours branch heavy code since those are correctly predicted.

but even if you care about a branch-predicted microbenchmark, you
made a single measurement per size so you cannot tell how much the
time varies, you should do several measurements and take the min
so noise from system effects and cpu internal state are reduced
(also that state needs to be warmed up). and likely the LOOP_TIMES
should be bigger too for small sizes for reliable timing.

benchmarking string functions is tricky especially for a target arch
with many implementations.

>    }
> 
>    printf("function test end\n");
>    munmap(str1,DATA_SIZE);
>    return 0;
> }
> 


  reply	other threads:[~2023-04-19  9:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-11  2:17 张飞
2023-04-11  9:48 ` Pedro Falcato
2023-04-19  5:33   ` 张飞
2023-04-19  9:02     ` Szabolcs Nagy [this message]
2023-04-20  8:17       ` 张飞
2023-04-21 13:30         ` Szabolcs Nagy
2023-04-21 14:50           ` Pedro Falcato
2023-04-21 16:54             ` Rich Felker
2023-04-21 17:01               ` enh
2023-04-26  7:25           ` 张飞

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230419090210.GR3630668@port70.net \
    --to=nsz@port70.net \
    --cc=musl@lists.openwall.com \
    --cc=zhangfei@nj.iscas.ac.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).