Cell.Calculateメソッドの計算時間を短縮

考えられる使用シナリオ

通常、ユーザーは電話することをお勧めしますWorkbook.CalculateFormula() メソッドを 1 回実行してから、個々のセルの計算値を取得します。しかし、ユーザーがワークブック全体を計算したくない場合があります。単一のセルを計算したいだけです。 Aspose.Cells提供CalculationOptions.Recursive設定できるプロパティ間違い個々のセルの計算時間を大幅に短縮します。 recursive プロパティが真実、その後、セルのすべての従属が各呼び出しで再計算されます。ただし、再帰プロパティが設定されている場合間違いの場合、依存セルは 1 回だけ計算され、その後の呼び出しでは再度計算されません。

Cell.Calculate()メソッドの計算時間を短縮

次のサンプル コードは、CalculationOptions.Recursive財産。指定されたコードでこのコードを実行してくださいサンプルエクセルファイルコンソール出力を確認します。再帰プロパティを間違い計算時間を大幅に短縮しました。このプロパティをよりよく理解するために、コメントもお読みください。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
public static void main(String[] args) throws Exception {
// Test calculation time after setting recursive true
TestCalcTimeRecursive(true);
// Test calculation time after setting recursive false
TestCalcTimeRecursive(false);
}
// --------------------------------------------------
static void TestCalcTimeRecursive(boolean rec) throws Exception {
String dataDir = Utils.getDataDir(DecreaseCalculationTime.class);
// Load your sample workbook
Workbook wb = new Workbook(dataDir + "sample.xlsx");
// Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
// Set the calculation option, set recursive true or false as per parameter
CalculationOptions opts = new CalculationOptions();
opts.setRecursive(rec);
// Start calculating time in nanoseconds
long startTime = System.nanoTime();
// Calculate cell A1 one million times
for (int i = 0; i < 1000000; i++) {
ws.getCells().get("A1").calculate(opts);
}
// Calculate elapsed time in seconds
long second = 1000000000;
long estimatedTime = System.nanoTime() - startTime;
estimatedTime = estimatedTime / second;
// Print the elapsed time in seconds
System.out.println("Recursive " + rec + ": " + estimatedTime + " seconds");
}

コンソール出力

これは、指定されたコマンドで実行したときの上記のサンプル コードのコンソール出力です。サンプルエクセルファイル私たちのマシンで。出力は異なる場合がありますが、再帰プロパティをに設定した後の経過時間に注意してください間違いに設定するよりも常に少なくなります。真実.

 Recursive true: 51 seconds

Recursive false: 16 seconds