【ハルシオンブログ】Listの全件検索をする場合、FindAllとforeachどっちが早いの!?
こんにちは。
涼しい日が続いてますね。
夏終わりましたかね?
さて、UnityでListとかを使ってる時に、FindAllを使うことが多々あります。
「FindAllってはやいの?」
と疑問がでたので、foreachと比較してみました。
コードはこちら
[Blog20210906.cs]
こんなことをした結果はこちら。
Calc1 : 81ms
Calc2 : 227ms
foreachよりFindAllのほうが断然早いですね!
あんまりforeach使ってた方少ないと思いますが、FindAllが早いのが分かりましたね。
というメモでした。
あでゅ~ノシ
涼しい日が続いてますね。
夏終わりましたかね?
さて、UnityでListとかを使ってる時に、FindAllを使うことが多々あります。
「FindAllってはやいの?」
と疑問がでたので、foreachと比較してみました。
コードはこちら
[Blog20210906.cs]
using System.Collections.Generic;
using UnityEngine;
public class Blog20210906 : MonoBehaviour
{
List<int> aryInt = new List<int>();
void Start()
{
for (int i = 0; i < 10000000; i++) {
aryInt.Add(i);
}
Calc1();
Calc2();
}
void Calc1() {
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
List<int> calc = aryInt.FindAll(x => x % 5 == 0);
sw.Stop();
Debug.Log("Calc1 : " + sw.ElapsedMilliseconds);
}
void Calc2() {
System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
sw2.Start();
List<int> calc2 = new List<int>();
foreach (int i in aryInt) {
if (i % 5 == 0) {
calc2.Add(i);
}
}
sw2.Stop();
Debug.Log("Calc2 : " + sw2.ElapsedMilliseconds);
}
}
こんなことをした結果はこちら。
Calc1 : 81ms
Calc2 : 227ms
foreachよりFindAllのほうが断然早いですね!
あんまりforeach使ってた方少ないと思いますが、FindAllが早いのが分かりましたね。
というメモでした。
あでゅ~ノシ
Category: 開発日記(Unity)
【ハルシオンブログ】Listから条件を指定してオブジェクトを取り出すFindの使い方の紹介
おはようございます。坂内っす。
今週末からゴールデンウィークですね!
コロナのせいでなにもできないGWになりそうですが!
さて、C#でList中のものを条件を指定して、取り出す方法として、Findがあります。
Find、FindAll、FindLastを簡単に紹介。
[Blog0426.cs]
上記のコードを実行するとこんな感じになります。

それぞれの条件で取り出せてますね。
ということで、Listから指定したオブジェクトの取り出し方の紹介でしたー
あでゅ~ノシ
今週末からゴールデンウィークですね!
コロナのせいでなにもできないGWになりそうですが!
さて、C#でList中のものを条件を指定して、取り出す方法として、Findがあります。
Find、FindAll、FindLastを簡単に紹介。
[Blog0426.cs]
using System.Collections.Generic;
using UnityEngine;
public class Blog0426 : MonoBehaviour
{
void Start() {
var list = new List<string>() { "あいう", "かきくけこ", "たなか", "やまぐち", "たちかわ" };
// 3文字のものを取得
Debug.Log("3文字のものをはじめから: " + list.Find(s => s.Length == 3));
// 3文字のものを取得(後ろから)
Debug.Log("3文字のものを後ろから : " + list.FindLast(s => s.Length == 3));
// 3文字のものをすべて取得
var all3obj = list.FindAll(s => s.Length == 3);
Debug.Log("3文字全部: ");
foreach (var obj in all3obj) {
Debug.Log("{0} " + obj);
}
// "か"を含むすべてを取得
var allKAobj = list.FindAll(s => s.Contains("か"));
Debug.Log("かが含まれる全部: ");
foreach (var obj in allKAobj) {
Debug.Log("{0} " + obj);
}
}
}
上記のコードを実行するとこんな感じになります。

それぞれの条件で取り出せてますね。
ということで、Listから指定したオブジェクトの取り出し方の紹介でしたー
あでゅ~ノシ
Category: 開発日記(Unity)
【ハルシオンブログ】Listの特定のアイテムを取り出す時って、Findを使ってますか?Linqを使ってますか?
こんにちは!坂内です!
UnityでListから特定のアイテムを取り出す場合、Find使ってますか?Linqを使ってますか?
Findを使う場合はこんな感じ。
(複数取り出すのでFindAllを使ってます)
[TestClass.cs]

同じことはLinqでもできますね。
LinqではFindの代わりにWhereを使うことで特定のアイテムを取り出すことが可能です。
LinqはUsing System.Linq;を書かないといけません。
また、戻り値はListではなくIEnumerableになります。
使いやすいほうを是非お使いください。
ってことで、Listデータの絞込についてでした!あでゅ~ノシ
UnityでListから特定のアイテムを取り出す場合、Find使ってますか?Linqを使ってますか?
Findを使う場合はこんな感じ。
(複数取り出すのでFindAllを使ってます)
[TestClass.cs]
using System.Collections.Generic;
using UnityEngine;
public class Test0323 : MonoBehaviour
{
public class TestClass {
public int no;
public string name;
}
public List<TestClass> testClasses;
void Start()
{
testClasses = new List<TestClass>();
// 初期データ
for(int i = 0; i < 10; i++) {
TestClass testClass = new TestClass();
testClass.no = i;
testClass.name = "Name:" + i;
testClasses.Add(testClass);
}
ViewTestClass(testClasses);
// iが2と5の物だけを出力
List classes25 = new List<TestClass>();
classes25 = testClasses.FindAll(x => x.no == 2 || x.no == 5);
ViewTestClass(classes25);
}
void ViewTestClass(List<TestClass> args) {
Debug.Log("-----------------------------------------------");
foreach(TestClass testClass in args) {
Debug.Log("No:" + testClass.no + "/Name:" + testClass.name);
}
}
}

同じことはLinqでもできますね。
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class Test0323 : MonoBehaviour
{
public class TestClass {
public int no;
public string name;
}
public List<TestClass> testClasses;
void Start()
{
testClasses = new List<TestClass>();
// 初期データ
for(int i = 0; i < 10; i++) {
TestClass testClass = new TestClass();
testClass.no = i;
testClass.name = "Name:" + i;
testClasses.Add(testClass);
}
ViewTestClass(testClasses);
// iが2と5の物だけを出力
var classes25 = testClasses.Where(x => x.no == 2 || x.no == 5);
ViewTestClass(classes25);
}
void ViewTestClass(IEnumerable<TestClass> args) {
Debug.Log("-----------------------------------------------");
foreach(TestClass testClass in args) {
Debug.Log("No:" + testClass.no + "/Name:" + testClass.name);
}
}
}
LinqではFindの代わりにWhereを使うことで特定のアイテムを取り出すことが可能です。
LinqはUsing System.Linq;を書かないといけません。
また、戻り値はListではなくIEnumerableになります。
使いやすいほうを是非お使いください。
ってことで、Listデータの絞込についてでした!あでゅ~ノシ
Category: 開発日記(Unity)
【ハルシオンブログ】ListでほしいものをFindするときに、まとめてとる方法や、Findの複数条件が使えることについて
こんにちは!坂内っす。
UnityでList使ってますか?
List<クラス>
で、指定クラスのオブジェクトがいっぱい入るリスト作れますよね。
よく使ってます。
例えばこんなクラスがあるとします。
【AAA.cs】
で、こんな感じで3個のAAAオブジェクトがListに入れます。
以前の記事で、Findを使って1つのオブジェクトを取り出すっていうのはやっています。
例えば「aaa.Noが2のモノを取り出す」って場合はこんな感じ
これで、a1として入れたものが取り出せます。
では「aaaTypeが0のモノをすべて」取り出すにはどうすればいいでしょうか?
こんな感じで複数のモノも取れます。
また、複数の検索条件ももちろん使うことができます。
これであなたもFindマスター(だったらいいよね)
ってことで、是非ListとFindをご使用ください。多様していいのか分かりませんが!
あでゅ~ノシ
UnityでList使ってますか?
List<クラス>
で、指定クラスのオブジェクトがいっぱい入るリスト作れますよね。
よく使ってます。
例えばこんなクラスがあるとします。
【AAA.cs】
public class AAA
{
public int aaaNo;
public int aaaType;
public string aaaName;
}
で、こんな感じで3個のAAAオブジェクトがListに入れます。
using System.Collections.Generic;
using UnityEngine;
public class Scripts20190708 : MonoBehaviour
{
List aaaAry = new List();
void Start()
{
AAA a0 = new AAA();
a0.aaaNo = 0;
a0.aaaType = 0;
a0.aaaName = "あいうえお";
aaaAry.Add(a0);
AAA a1 = new AAA();
a1.aaaNo = 1;
a1.aaaType = 0;
a1.aaaName = "かきくけこ";
aaaAry.Add(a1);
AAA a2 = new AAA();
a2.aaaNo = 2;
a2.aaaType = 1;
a2.aaaName = "さしすせそ";
aaaAry.Add(a2);
}
}
以前の記事で、Findを使って1つのオブジェクトを取り出すっていうのはやっています。
例えば「aaa.Noが2のモノを取り出す」って場合はこんな感じ
AAA no2Object = aaaAry.Find(x => x.aaaNo == 2);
これで、a1として入れたものが取り出せます。
では「aaaTypeが0のモノをすべて」取り出すにはどうすればいいでしょうか?
List allType0Obj = aaaAry.FindAll(x => x.aaaType == 0);
こんな感じで複数のモノも取れます。
また、複数の検索条件ももちろん使うことができます。
AAA no2Type1Obj = aaaAry.Find(x => x.aaaNo == 2 && x.aaaType == 1);
これであなたもFindマスター(だったらいいよね)
ってことで、是非ListとFindをご使用ください。多様していいのか分かりませんが!
あでゅ~ノシ
Category: 開発日記(Unity)
【ハルシオンブログ】UnityというかC#のListについて。最近使った親Listへの代入方法。
こんにちは。坂内っす。
今日はListについて。
ゲームを作っていると、同じクラスなどをまとめて格納したい時ってありますよね。
配列でもいいんですが、配列だと要素数増やしたりするのがめんどいので最近はいつもListを使ってます。
intなんかも入っちゃう優れもの。
作ったクラスもなんでも入っちゃう
Find関数を使うと、このListから「特定条件のものだけ取り出す」などの処理も簡単にできます。
【検索】
"あいうえお"が表示されます。
Findの中で検索をしています。
これが条件。ラムダ式を使う感じです。
「x => x.no == 0」
【継承クラスの格納】
子供のクラスを格納までは簡単です。
ほら、普通に入る。
それでは、ChildとChild2のListを別々に作ってParentのListへ代入する場合は?
これエラー出ます。
そのままでは代入できないんですよね。
そこで使うのがこれ。
Cast<親クラス>().ToList();
これを使うことで、親クラスにキャスト(型変換)して親のListに入れることができます。
※Cast関数を使うには、using System.Linq;が必要になります。
どこで使うかはわかりませんが、今作ってるゲームでこれ使う機会あったので簡単に紹介しておきました。
それでは あでゅ~ノシ
今日はListについて。
ゲームを作っていると、同じクラスなどをまとめて格納したい時ってありますよね。
配列でもいいんですが、配列だと要素数増やしたりするのがめんどいので最近はいつもListを使ってます。
intなんかも入っちゃう優れもの。
private void Start() {
List<int> intList = new List<int>();
intList.Add(11);
intList.Add(8);
intList.Add(2);
}
作ったクラスもなんでも入っちゃう
using System.Collections.Generic;
using UnityEngine;
public class ListTest : MonoBehaviour {
public class TestClass {
public int no;
public string moji;
}
private void Start() {
List<int> intList = new List<int>();
intList.Add(11);
intList.Add(8);
intList.Add(2);
List<TestClass> testAry = new List<TestClass>();
TestClass item1 = new TestClass();
item1.no = 0;
item1.moji = "あいうえお";
testAry.Add(item1);
TestClass item2 = new TestClass();
item2.no = 1;
item2.moji = "かきくけこ";
testAry.Add(item2);
}
}
Find関数を使うと、このListから「特定条件のものだけ取り出す」などの処理も簡単にできます。
【検索】
public class TestClass {
public int no;
public string moji;
}
private void Start() {
List<int> intList = new List<int>();
intList.Add(11);
intList.Add(8);
intList.Add(2);
List<TestClass> testAry = new List<TestClass>();
TestClass item1 = new TestClass();
item1.no = 0;
item1.moji = "あいうえお";
testAry.Add(item1);
TestClass item2 = new TestClass();
item2.no = 1;
item2.moji = "かきくけこ";
testAry.Add(item2);
// 検索
TestClass findItem = testAry.Find(x => x.no == 0);
Debug.Log(findItem.moji);
"あいうえお"が表示されます。
Findの中で検索をしています。
これが条件。ラムダ式を使う感じです。
「x => x.no == 0」
【継承クラスの格納】
子供のクラスを格納までは簡単です。
public class ParentClass {
public int no;
public string moji;
}
public class ChildClass : ParentClass {
public string name;
}
public class ChildClass2 : ParentClass {
public string name2;
}
private void Start() {
ChildClass child = new ChildClass();
child.no = 0;
child.moji = "ああ";
child.name = "チャイルド";
ChildClass2 child2 = new ChildClass2();
child2.no = 1;
child2.moji = "いい";
child2.name2 = "チャイルド2";
List<ParentClass> parentAry = new List<ParentClass>();
parentAry.Add(child);
parentAry.Add(child2);
}
ほら、普通に入る。
それでは、ChildとChild2のListを別々に作ってParentのListへ代入する場合は?
public class ParentClass {
public int no;
public string moji;
}
public class ChildClass : ParentClass {
public string name;
}
public class ChildClass2 : ParentClass {
public string name2;
}
private void Start() {
List<ChildClass> childAry = new List<ChildClass>();
ChildClass child1_1 = new ChildClass();
child1_1.no = 0;
child1_1.moji = "ああ";
child1_1.name = "チャイルド1-1";
childAry.Add(child1_1);
ChildClass child1_2 = new ChildClass();
child1_1.no = 1;
child1_1.moji = "あああ";
child1_1.name = "チャイルド1-2";
childAry.Add(child1_2);
List<ChildClass2> child2Ary = new List<ChildClass2>();
ChildClass2 child2_1 = new ChildClass2();
child2_1.no = 0;
child2_1.moji = "いい";
child2_1.name2 = "チャイルド2-1";
child2Ary.Add(child2_1);
ChildClass2 child2_2 = new ChildClass2();
child2_2.no = 0;
child2_2.moji = "いいい";
child2_2.name2 = "チャイルド2-2";
child2Ary.Add(child2_2);
List<ParentClass> parentAry = new List<ParentClass>();
parentAry = childAry;
}
これエラー出ます。
そのままでは代入できないんですよね。
そこで使うのがこれ。
parentAry = childAry.Cast<ParentClass>().ToList();
Cast<親クラス>().ToList();
これを使うことで、親クラスにキャスト(型変換)して親のListに入れることができます。
※Cast関数を使うには、using System.Linq;が必要になります。
どこで使うかはわかりませんが、今作ってるゲームでこれ使う機会あったので簡単に紹介しておきました。
それでは あでゅ~ノシ
Category: 開発日記(Unity)