Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #126346

Re: Count string in string with loop increase 2, why is this not faster?

Subject Re: Count string in string with loop increase 2, why is this not faster?
Newsgroups comp.lang.c
References (12 earlier) <j15eC.18818$s_2.335@fx42.iad> <8C7eC.446657$1t7.45854@fx03.am4> <f93a9bfe-a458-494b-a8fb-78165b6949d2@googlegroups.com> <s9meC.917167$ch5.593863@fx18.am4> <2297ba4e-8d2f-4516-9907-b767f2836897@googlegroups.com>
From bartc <bc@freeuk.com>
Message-ID <aToeC.470372$oD1.406285@fx44.am4> (permalink)
Organization virginmedia.com
Date 2018-02-06 21:12 +0000

Show all headers | View raw


On 06/02/2018 20:34, Bart Smissaert wrote:
> No idea why your differences are so much larger. I am compiling (std_call as I am accessing the C dll from VBA via a .tlb) in MSVS 2012. Not sure it makes a difference, but my code is in the large SQLite3 amalgation 3.22.0.
> 
> I had a go at trying to make a fast function for if strFind is a single character, but this is the best I have come up with and not much faster than your latest test1:
> 
> SQLITE_API int sqlite3_CountCharInString(const wchar_t *strSearch, int count, const wchar_t *strChar)
> {
> 	int i;
> 	int total;
> 	total = 0;
> 
> 	for (i = 0; i<count; i++)
> 	{
> 		if (*strSearch == *strChar) total++;
> 		strSearch++;
> 	}
> 	return total;
> }
> 
> RBS
> 

This is very odd.

My routine took 2.8 seconds to find the 12 occurrences of "t" in your 
test string (repeated 10 milion times).

Then I tried the specialised routine below, which is simpler than yours, 
doesn't need the string length (assuming it's zero-terminated with a 
16-bit zero), and passes the find character directly.

It took 1.2 seconds for the same task (either called directly, or from 
inside the general routine).

Then I thought I'd try yours. And yours took 0.4 seconds!

So I've no idea what's going on.

I then decided to leave gcc and tried a non-optimising compiler, TCC. 
There, the regular routine took 4.3 seconds; my testonechar() 3.4 
seconds, and yours was 4.5 seconds.

Using gcc again with optimisation off, the results were similar to TCC, 
but narrower.

So it might be better to disregard the results from gcc-O3, as that 
might be taking advantage of some inferences it can make from the small 
test program, which will not be the case in a real program.

It looks like my one-character routine is somewhat faster than the 
regular one; yours /might/ be faster. But in all it probably isn't 
worthwhile to have a dedicated function.

(Do you get your MSVC to optimise the code?)

--------------------------------
int testonechar(wchar_t *strSearch, wchar_t cFind)
{
     int c;
     int countstring = 0;

     while (c  = *strSearch++) if (c==cFind) ++countstring;

     return countstring;
}

-- 
bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-03 00:00 -0800
  Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-03 11:29 +0000
    Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-03 03:37 -0800
      Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-03 12:29 +0000
        Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-03 04:37 -0800
          Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-03 13:41 +0000
            Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-03 06:38 -0800
              Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-03 15:13 +0000
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-03 15:19 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-03 07:41 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-03 11:38 -0500
          Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-03 13:50 +0000
  Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-03 11:34 +0000
    Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-03 11:32 -0500
      Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-03 16:41 +0000
      Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-03 09:02 -0800
        Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-03 09:06 -0800
        Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-03 12:39 -0500
          Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-03 11:56 -0800
            Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-03 19:54 -0500
              Re: Count string in string with loop increase 2, why is this not faster? Keith Thompson <kst-u@mib.org> - 2018-02-03 18:00 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-04 01:53 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-04 13:23 -0500
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-04 11:11 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-04 14:48 -0500
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-04 12:17 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-04 21:53 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-04 14:35 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-04 23:32 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-05 06:26 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-05 16:59 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-05 09:44 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-05 18:28 +0000
                Re: Count string in string with loop increase 2, why is this not faster? scott@slp53.sl.home (Scott Lurndal) - 2018-02-05 18:41 +0000
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-05 20:08 +0000
                Re: Count string in string with loop increase 2, why is this not faster? scott@slp53.sl.home (Scott Lurndal) - 2018-02-05 21:27 +0000
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-05 22:04 +0000
                Re: Count string in string with loop increase 2, why is this not faster? scott@slp53.sl.home (Scott Lurndal) - 2018-02-05 22:38 +0000
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-06 01:34 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-06 06:39 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-06 18:07 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-06 12:34 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-06 21:12 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-06 13:42 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-06 16:43 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-07 01:10 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Keith Thompson <kst-u@mib.org> - 2018-02-06 18:47 -0800
                Re: Count string in string with loop increase 2, why is this not faster? David Brown <david.brown@hesbynett.no> - 2018-02-07 15:13 +0100
                Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-07 14:48 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-07 17:13 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-07 17:27 +0000
                Re: Count string in string with loop increase 2, why is this not faster? David Brown <david.brown@hesbynett.no> - 2018-02-07 17:01 +0100
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-07 02:00 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-07 12:02 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-07 14:46 +0000
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-07 13:56 +0000
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-07 11:17 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-06 23:59 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-07 05:10 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-07 11:01 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-07 14:24 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-07 14:44 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-07 22:59 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Keith Thompson <kst-u@mib.org> - 2018-02-07 15:20 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-07 23:31 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-07 15:32 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Keith Thompson <kst-u@mib.org> - 2018-02-07 16:05 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-05 15:29 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-04 17:41 -0500
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-04 23:11 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-04 23:45 -0500
                Re: Count string in string with loop increase 2, why is this not faster? Keith Thompson <kst-u@mib.org> - 2018-02-04 15:11 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-04 23:37 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Keith Thompson <kst-u@mib.org> - 2018-02-04 18:08 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-04 23:12 -0500
                Re: Count string in string with loop increase 2, why is this not faster? David Brown <david.brown@hesbynett.no> - 2018-02-05 13:08 +0100
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-05 08:24 -0500
                Re: Count string in string with loop increase 2, why is this not faster? David Brown <david.brown@hesbynett.no> - 2018-02-05 14:39 +0100
                Re: Count string in string with loop increase 2, why is this not faster? Robert Wessel <robertwessel2@yahoo.com> - 2018-02-05 10:37 -0600
                Re: Count string in string with loop increase 2, why is this not faster? supercat@casperkitty.com - 2018-02-05 13:52 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-05 22:29 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Keith Thompson <kst-u@mib.org> - 2018-02-05 09:01 -0800
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-05 18:44 +0000
                Re: Count string in string with loop increase 2, why is this not faster? David Brown <david.brown@hesbynett.no> - 2018-02-06 10:42 +0100
                Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-06 11:17 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-06 08:40 -0500
                Re: Count string in string with loop increase 2, why is this not faster? bartc <bc@freeuk.com> - 2018-02-06 14:08 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-06 14:42 +0000
                Re: Count string in string with loop increase 2, why is this not faster? Keith Thompson <kst-u@mib.org> - 2018-02-06 12:35 -0800
                Re: Count string in string with loop increase 2, why is this not faster? David Brown <david.brown@hesbynett.no> - 2018-02-06 23:04 +0100
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-04 17:35 -0500
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-04 14:38 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-04 17:57 -0500
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-04 15:22 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Bart Smissaert <bart.smissaert@gmail.com> - 2018-02-04 12:19 -0800
                Re: Count string in string with loop increase 2, why is this not faster? Richard Damon <Richard@Damon-Family.org> - 2018-02-04 16:42 -0500
          Re: Count string in string with loop increase 2, why is this not faster? Keith Thompson <kst-u@mib.org> - 2018-02-03 13:38 -0800
  Re: Count string in string with loop increase 2, why is this not faster? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-03 22:03 +0000

csiph-web