PCEP Profiling Practice Question
A team is writing a Python script that reads a large log file and counts occurrences of ERROR. The script works but is very slow. They profile it and find that most time is spent reading the file line by line. Which optimization technique is most appropriate?
⚠ Common exam trap
A common trap is to assume that because profiling attributes time to file reading, you should optimize the reading method (e.g., using readlines() or binary mode). However, the real gain often comes from optimizing the inner loop logic, as the reading overhead is unavoidable and proportional to file size.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Examine the inner loop logic for unnecessary operations per line.
Profiling shows that most time is spent on reading the file line by line, which indicates an I/O bottleneck. However, among the given optimization techniques, examining the inner loop logic (Option B) is the most appropriate, as it can uncover unnecessary per‑line operations (e.g., redundant string methods, repeated allocations) that, while not the primary bottleneck, may be compounding the I/O wait time. The other options are less suitable: Option A risks memory issues with large files, Option C typically provides negligible performance improvement, and Option D is a syntax enhancement with no performance impact.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use .readlines() to load the entire file into memory at once.
Why it's wrong here
This increases memory usage and may not speed up processing.
- ✓
Examine the inner loop logic for unnecessary operations per line.
Why this is correct
Optimizing per-line operations rather than file reading is likely the key.
- ✗
Open the file in binary mode to reduce overhead.
Why it's wrong here
Binary mode does not improve text line processing.
- ✗
Rewrite the script using the 'with' statement for file handling.
Why it's wrong here
The 'with' statement is already best practice but does not affect speed.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.