تحسين استخدام الذاكرة أثناء العمل مع ملفات كبيرة تحتوي على مجموعات بيانات كبيرة
عند بناء دفتر عمل مع مجموعات بيانات كبيرة، أو قراءة ملف Microsoft Excel كبير، يكون المبلغ الإجمالي لذاكرة الوصول العشوائي التي سيستغرقها العملية دائمًا محل اهتمام. هناك إجراءات يمكن اعتمادها للتعامل مع التحدي. يوفر Aspose.Cells بعض الخيارات القابلة للتكيف واستدعاءات واجهة برمجة التطبيقات ذات الصلة لتقليل وتقليل وتحسين استخدام الذاكرة. بالإضافة إلى ذلك، يمكن أن يساعد في جعل العملية تعمل بكفاءة أكبر وتشغيلها بصورة أسرع.
استخدام الخيار MemorySetting.MemoryPreference لتحسين استخدام الذاكرة لبيانات الخلايا وتقليل التكلفة الإجمالية للذاكرة. عند بناء مجموعة بيانات كبيرة للخلايا، يمكن أن يوفر مبلغًا معينًا من الذاكرة بالمقارنة بالاستخدام الافتراضي (MemorySetting.Normal).
تحسين الذاكرة
قراءة ملفات Excel الكبيرة
توضح المثال التالي كيفية قراءة ملف إكسل كبير بوضع محسن.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Specify the LoadOptions | |
LoadOptions opt = new LoadOptions(); | |
// Set the memory preferences | |
opt.MemorySetting = MemorySetting.MemoryPreference; | |
// Instantiate the Workbook | |
// Load the Big Excel file having large Data set in it | |
Workbook wb = new Workbook(dataDir+ "Book1.xlsx", opt); |
كتابة ملفات إكسيل الكبيرة
المثال التالي يوضح كيفية كتابة مجموعة بيانات كبيرة إلى ورقة عمل بوضع محسن.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a new Workbook | |
Workbook wb = new Workbook(); | |
// Set the memory preferences | |
// Note: This setting cannot take effect for the existing worksheets that are created before using the below line of code | |
wb.Settings.MemorySetting = MemorySetting.MemoryPreference; | |
// Note: The memory settings also would not work for the default sheet i.e., "Sheet1" etc. automatically created by the Workbook | |
// To change the memory setting of existing sheets, please change memory setting for them manually: | |
Cells cells = wb.Worksheets[0].Cells; | |
cells.MemorySetting = MemorySetting.MemoryPreference; | |
// Input large dataset into the cells of the worksheet. | |
// Your code goes here. | |
// ......... | |
// Get cells of the newly created Worksheet "Sheet2" whose memory setting is same with the one defined in WorkbookSettings: | |
cells = wb.Worksheets.Add("Sheet2").Cells; | |
// ......... | |
// Input large dataset into the cells of the worksheet. | |
// Your code goes here. | |
// ......... |
احترس
الخيار الافتراضي، MemorySetting.Normal يُطبق على جميع الإصدارات. لبعض الحالات، مثل بناء جدول عمل مع مجموعة بيانات كبيرة للخلايا، يمكن أن يُحسن الخيار MemorySetting.MemoryPreference استخدام الذاكرة ويُقلل من تكلفة الذاكرة للتطبيق. ومع ذلك، قد يُتدنى أداء هذا الخيار في بعض الحالات الخاصة مثلما يلي.
- الوصول العشوائي والمتكرر إلى الخلايا: أكثر تسلسل فعالية للوصول إلى مجموعة الخلايا هو الخلية بالخلية في صف واحد، ثم صف بعد صف. خاصة إذا أمكنك الوصول إلى الصفوف/الخلايا من خلال المدرج المصرف من Cells، RowCollection و Row، سيتم تحقيق أقصى أداء مع MemorySetting.MemoryPreference.
- إدراج وحذف الخلايا والصفوف: يرجى ملاحظة أنه إذا كانت هناك الكثير من عمليات الإدراج/الحذف للخلايا/الصفوف، سيكون التدهور في الأداء ملحوظًا في وضع تفضيل الذاكرة مقارنةً بوضع طبيعي.
- العمل على أنواع الخلايا المختلفة: إذا كانت معظم الخلايا تحتوي على قيم سلسلة أو صيغًا، ستكون تكلفة الذاكرة نفسها كوضع طبيعي ولكن إذا كانت هناك الكثير من الخلايا الفارغة، أو قيم الخلايا تكون رقمية، بولية وما إلى ذلك، فإن الخيار MemorySetting.MemoryPreference سيمنح أداءً أفضل.