Performance and Optimization Flashcards

1
Q

What is code profiling, and why is it important for optimizing performance?

A

Code profiling involves analyzing the execution of your code to identify performance bottlenecks and areas for improvement. It helps ensure your application runs efficiently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Name some common performance bottlenecks in .NET applications.

A

Examples include CPU-bound operations, excessive memory usage, inefficient database queries, and excessive I/O operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What tools can you use for code profiling in .NET applications?

A

Profilers like JetBrains dotTrace, Visual Studio Profiler, and the built-in Performance Profiler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the difference between CPU-bound and I/O-bound operations.

A

CPU-bound operations are limited by the processing power of the CPU, while I/O-bound operations are limited by input/output operations (e.g., reading/writing files, database queries).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you optimize CPU-bound operations?

A

Techniques include parallel processing, using efficient algorithms, and optimizing loops to reduce unnecessary calculations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the N+1 query problem, and how can it be optimized in database operations?

A

The N+1 query problem occurs when fetching related data from a database results in multiple individual queries. It can be optimized using eager loading or using JOIN statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain JIT (Just-In-Time) compilation and how it impacts performance.

A

JIT compilation converts MSIL (Intermediate Language) into native machine code at runtime. It might introduce a slight overhead during startup but can lead to better performance over time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Discuss the concept of Amdahl’s Law in relation to code optimization.

A

Amdahl’s Law states that the potential speedup of a program is limited by the portion of the code that cannot be parallelized. It emphasizes the importance of optimizing critical sections.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is caching, and why is it essential for performance optimization?

A

Caching involves storing frequently accessed data in memory to reduce the need to fetch it from slower sources. It improves response times and reduces load on databases or other resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain in-memory caching and its benefits.

A

In-memory caching stores data in the application’s memory, allowing for faster access compared to fetching data from external sources. It’s well-suited for frequently accessed data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s the difference between client-side caching and server-side caching?

A

Client-side caching involves storing data on the client’s device, while server-side caching stores data on the server. Server-side caching is more controlled and can be shared among users.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you implement distributed caching in a .NET application?

A

You could use technologies like Redis or Microsoft’s Azure Cache for distributed caching, which allows you to share cached data across multiple instances of your application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are cache expiration policies, and why are they important?

A

Cache expiration policies define when cached data should be invalidated or refreshed. They are essential to ensure that the cached data remains accurate and up-to-date.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain cache stampede and how it can be mitigated.

A

Cache stampede occurs when a cache entry expires, and multiple requests simultaneously try to repopulate it, causing a spike in resource usage. It can be mitigated using techniques like cache locking or staggered expiration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is asynchronous programming, and why is it crucial for improving application responsiveness?

A

Asynchronous programming allows tasks to run independently, enabling the application to continue executing other tasks while waiting for slow operations (I/O, network calls) to complete.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Compare synchronous and asynchronous programming in .NET

A

Synchronous programming blocks the execution thread until a task is completed, while asynchronous programming enables the thread to perform other work while waiting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the main benefits of using the async and await keywords in C#?

A

async and await keywords make it easier to write asynchronous code that is more readable and maintainable. They enable the use of non-blocking operations without dealing with low-level callbacks.

18
Q

What is a deadlock in asynchronous programming, and how can you avoid it?

A

A deadlock occurs when two or more tasks are waiting for each other to complete. To avoid deadlocks, use asynchronous methods consistently and avoid blocking calls within asynchronous methods.

19
Q

Explain the concept of the Task Parallel Library (TPL) in .NET.

A

TPL provides a higher-level abstraction for writing parallel and asynchronous code, making it easier to work with tasks and parallelize operations.

20
Q

How can you handle exceptions in asynchronous code?

A

Use try-catch blocks around await statements to handle exceptions. Also, use the Task.Exception property to observe exceptions in a more controlled manner.

21
Q

Discuss the potential drawbacks of using asynchronous programming.

A

Asynchronous programming can introduce complexity and require careful management of shared resources. It’s important to balance the benefits with the potential drawbacks.

22
Q

What is the difference between hot and cold paths in terms of code optimization?

A

Hot paths are frequently executed sections of code that should be optimized for speed, while cold paths are less frequently executed and might not need as much optimization.

23
Q

Explain the concept of lazy loading and its impact on performance.

A

Lazy loading defers the loading of certain resources until they are actually needed, helping to improve the initial loading time of an application.

24
Q

How can you reduce memory consumption in a .NET application?

A

Techniques include optimizing data structures, disposing of objects properly, and avoiding memory leaks through careful resource management.

25
Q

Discuss the benefits and challenges of using value types (structs) over reference types (classes) for performance.

A

Value types can reduce memory overhead and improve cache locality, but they come with limitations on size and potential boxing/unboxing issues.

26
Q

What is the role of the Garbage Collector (GC) in .NET, and how can you optimize memory management?

A

The GC manages memory by reclaiming memory from objects that are no longer in use. Optimize memory management by minimizing object allocation and avoiding circular references.

27
Q

Explain the cache-aside (lazy loading) caching pattern.

A

In cache-aside caching, the application code is responsible for managing the cache. It fetches data from the cache and, if not present, loads it from the data source and then stores it in the cache.

28
Q

What is cache coherency, and why is it important in distributed caching?

A

Cache coherency ensures that all instances of a distributed cache have consistent data. It’s crucial to maintain data integrity across cache nodes.

29
Q

Discuss the differences between cache eviction and cache expiration.

A

Cache eviction involves removing items from the cache based on certain policies (LRU, LFU), while cache expiration involves setting a time limit after which cached items become invalid.

30
Q

How can you handle cache invalidation to ensure data consistency?

A

Implement cache invalidation strategies like TTL (Time to Live) or using a publish-subscribe mechanism to notify cache updates.

31
Q

What is a cache hit and a cache miss? How do they impact performance?

A

A cache hit occurs when requested data is found in the cache, while a cache miss occurs when it’s not. Cache hits significantly improve performance by avoiding costly data retrieval operations.

32
Q

Explain the concept of a “fire and forget” asynchronous operation

A

A fire and forget operation is one where you start an asynchronous task without waiting for its completion or without handling its result.

33
Q

What is the purpose of the ConfigureAwait method in asynchronous programming?

A

ConfigureAwait is used to control the context in which an awaited task resumes. It helps prevent deadlocks and improves efficiency by avoiding unnecessary context switches.

34
Q

Discuss the use of cancellation tokens in asynchronous programming.

A

Cancellation tokens allow you to cancel ongoing asynchronous operations gracefully, which is important for user responsiveness and resource management.

35
Q

What are thread pools, and how do they relate to asynchronous programming?

A

Thread pools manage a pool of worker threads that can be used for executing asynchronous tasks, reducing the overhead of creating new threads for every task.

36
Q

Explain the differences between parallelism and concurrency in the context of asynchronous programming.

A

Parallelism involves executing multiple tasks simultaneously, while concurrency is about managing multiple tasks concurrently, even if they’re not executing simultaneously.

37
Q

How can you ensure thread safety in asynchronous code?

A

Use synchronization mechanisms like lock, Monitor, or asynchronous coordination primitives like SemaphoreSlim to protect shared resources from concurrent access

38
Q

What is the difference between I/O-bound and compute-bound asynchronous operations?

A

I/O-bound operations spend most of their time waiting for external resources, while compute-bound operations spend more time executing calculations. Different optimization techniques apply to each.

39
Q

Explain the concept of pipelining in asynchronous programming.

A

Pipelining involves breaking down a complex task into multiple stages, where each stage is handled by a separate asynchronous operation. This can improve throughput and performance.

40
Q

What are the challenges of debugging asynchronous code, and how can you overcome them?

A

Debugging asynchronous code can be tricky due to non-deterministic behavior and complex call stacks. Use tools like the Visual Studio debugger and asynchronous-aware debugging techniques