View Single Post
Old 2009-03-13, 14:08   #3
fivemack
(loop (#_fork))
 
fivemack's Avatar
 
Feb 2006
Cambridge, England

2×3,191 Posts
Default

Hmm. sscanf behaves really, really badly on very long strings - it looks as if it does something strlen-like first, so if I use code like

Code:
  FILE* f = fopen("bibble","rb");
  char* world = new char[15889012];
  fread(world,1,15889012,f);
  fclose(f);

  unsigned long long J,K=0;
  u64 t0= rdtsc();
  char* ptr = world;
  for (int u=0; u<1000; u++)
    {
      sscanf(ptr,"%lld",&J);
      while (*ptr>=48 && *ptr<=57) ptr++;
      ptr++;
      K^=J;
    }
  u64 t1 = rdtsc();
  printf("%lld %lld\n",K,t1-t0);
I get timings like 20M cycles per sscanf. If I make sure that all the strings are null-terminated by something like

Code:
  FILE* f = fopen("bibble","rb");
  char* world = new char[15889012];
  fread(world,1,15889012,f);
  fclose(f);

  unsigned long long J,K=0;
  u64 t0= rdtsc();
  for (int u=0; u<15889012;u++) if (world[u]==10) world[u]=0;
  char* ptr = world;
  for (int u=0; u<1000000; u++)
    {
      sscanf(ptr,"%lld",&J);
      while (*ptr>=48 && *ptr<=57) ptr++;
      ptr++;
      K^=J;
    }
  u64 t1 = rdtsc();
  printf("%lld %lld\n",K,t1-t0);
it's down to a much saner ~900 cycles per sscanf.
fivemack is offline   Reply With Quote