Cell.Calculateメソッドの計算時間の短縮
可能な使用シナリオ
通常、ユーザーにはWorkbook.CalculateFormula()メソッドを一度呼び出し、個々のセルの計算値を取得することをお勧めします。しかし、時々、ユーザーはブック全体を計算したくない場合があります。単に個々のセルを計算したいだけです。Aspose.Cellsは、CalculationOptions.Recursive プロパティを提供しており、これをfalseに設定すると、個々のセルの計算時間が大幅に短縮されます。trueにプロパティが設定されている場合、セルの依存関係は各呼び出しに対して再計算されますが、falseにプロパティが設定されている場合、依存するセルは1度だけ計算され、次回以降に再計算されません。
Cell.Calculate() メソッドの計算時間を短縮する
次のサンプルコードは、CalculationOptions.Recursiveプロパティの使用法を示しています。指定されたサンプルExcelファイル を使用してこのコードを実行し、コンソール出力を確認してください。CalculationOptions.Recursiveプロパティをfalseに設定すると、計算時間が大幅に短縮されることがわかります。このプロパティの理解を深めるためにコメントもお読みください。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Test calculation time after setting recursive true | |
TestCalcTimeRecursive(true); | |
// Test calculation time after setting recursive false | |
TestCalcTimeRecursive(false); |
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
static void TestCalcTimeRecursive(bool rec) | |
{ | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load your sample workbook | |
Workbook wb = new Workbook(dataDir + "sample.xlsx"); | |
// Access first worksheet | |
Worksheet ws = wb.Worksheets[0]; | |
// Set the calculation option, set recursive true or false as per parameter | |
CalculationOptions opts = new CalculationOptions(); | |
opts.Recursive = rec; | |
// Start stop watch | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
// Calculate cell A1 one million times | |
for (int i = 0; i < 1000000; i++) | |
{ | |
ws.Cells["A1"].Calculate(opts); | |
} | |
// Stop the watch | |
sw.Stop(); | |
// Calculate elapsed time in seconds | |
long second = 1000; | |
long estimatedTime = sw.ElapsedMilliseconds / second; | |
// Print the elapsed time in seconds | |
Console.WriteLine("Recursive " + rec + ": " + estimatedTime + " seconds"); | |
} |
コンソール出力
上記のサンプルコードを指定されたサンプルExcelファイル で実行した際のコンソール出力です。出力は異なる可能性がありますが、再帰プロパティをfalseに設定した後の経過時間は常にtrueに設定するよりも少なくなります。
Recursive True: 96 seconds
Recursive False: 42 seconds