0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

部分文字列の出現回数を計算する処理(2回以上する出現する全ての部分文字列)における、組み込み関数の性能を比較してみます。ちなみに手法自体は素朴で馬鹿力任せのごみです。

//対象文字列
const txt=Object.getOwnPropertyNames(this).join(" ").slice(0,1<<12);

// indexOf法
function ss_index(s){
	for(var a=new Date,b=0,t=2,i,j,m={__proto__:null},p,f=1,z=s.length;f;++t)
		for(f=i=0;i<z-t;)
			if(!m[p=s.substr(j=i++,t)]&&~(j=s.indexOf(p,j+t))){
				for(f=2;~(j=s.indexOf(p,j+t));)f++;
				m[p]=f,b++
			}
	console.log(new Date-a,b)
}
// split法
function ss_split(s){
	for(var a=new Date,b=0,t=2,i,j,m={__proto__:null},p,f=1,z=s.length;f;++t)
		for(f=i=0;i<z-t;)
			if(!m[p=s.substr(j=i++,t)]&&~(j=s.indexOf(p,j+t)))
				m[p]=f=s.split(p).length-1,b++
	console.log(new Date-a,b)
}
// slice+split法
function ss_slice_split(s){
	for(var a=new Date,b=0,t=2,i,j,m={__proto__:null},p,f=1,z=s.length;f;++t)
		for(f=i=0;i<z-t;)
			if(!m[p=s.substr(j=i++,t)]&&~(j=s.indexOf(p,j+t)))
				m[p]=f=s.slice(j).split(p).length,b++
	console.log(new Date-a,b)
}
// indexOf法2
function ss_index2(s){
	for(var a=new Date,b=0,t=2,i,j,m=[],n=[],p,f=1,z=s.length;f;++t)
		for(f=i=0;i<z-t;)
			if(m.indexOf(p=s.substr(j=i++,t))<0&&~(j=s.indexOf(p,j+t))){
				for(f=2;~(j=s.indexOf(p,j+t));)f++;
				m[b]=p,n[b++]=f
			}
	console.log(new Date-a,b)
}
//Map法
function ss_map(s){
	for(var a=new Date,b=0,t=2,i,j,m=new Map,p,f=1,z=s.length;f;++t)
		for(f=i=0;i<z-t;)
			if(!m.has(p=s.substr(j=i++,t))&&~(j=s.indexOf(p,j+t))){
				for(f=2;~(j=s.indexOf(p,j+t));)f++;
				m.set(p,f),b++
			}
	console.log(new Date-a,b)
}
//test
for(let f of[ss_split,ss_slice_split,ss_map,ss_index,ss_index2])
	f(txt)

1匹だけ飛び抜けて遅い奴がいますな…。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?