How to Improve Code Performance

Explore top LinkedIn content from expert professionals.

Summary

Improving code performance means making computer programs run faster, use less memory, and respond more quickly to users. This involves smart programming choices that help applications handle tasks efficiently, whether in web development, APIs, or machine learning.

  • Use smart caching: Store frequently accessed data in memory so the program doesn’t keep repeating slow operations, dramatically cutting response times.
  • Reduce unnecessary work: Schedule non-critical tasks to run only when the computer or browser is idle, keeping the main features responsive and smooth.
  • Streamline your code: Remove unused code, compress files, and split large chunks into smaller pieces to speed up downloads and reduce delays in loading pages or processing data.
Summarized by AI based on LinkedIn member posts
  • View profile for Brij Kishore Pandey
    Brij Kishore Pandey Brij Kishore Pandey is an Influencer

    AI Architect & AI Engineer | Building Agentic Systems & Scalable AI Solutions

    727,405 followers

    A sluggish API isn't just a technical hiccup – it's the difference between retaining and losing users to competitors. Let me share some battle-tested strategies that have helped many  achieve 10x performance improvements: 1. 𝗜𝗻𝘁𝗲𝗹𝗹𝗶𝗴𝗲𝗻𝘁 𝗖𝗮𝗰𝗵𝗶𝗻𝗴 𝗦𝘁𝗿𝗮𝘁𝗲𝗴𝘆 Not just any caching – but strategic implementation. Think Redis or Memcached for frequently accessed data. The key is identifying what to cache and for how long. We've seen response times drop from seconds to milliseconds by implementing smart cache invalidation patterns and cache-aside strategies. 2. 𝗦𝗺𝗮𝗿𝘁 𝗣𝗮𝗴𝗶𝗻𝗮𝘁𝗶𝗼𝗻 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻 Large datasets need careful handling. Whether you're using cursor-based or offset pagination, the secret lies in optimizing page sizes and implementing infinite scroll efficiently. Pro tip: Always include total count and metadata in your pagination response for better frontend handling. 3. 𝗝𝗦𝗢𝗡 𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝗢𝗽𝘁𝗶𝗺𝗶𝘇𝗮𝘁𝗶𝗼𝗻 This is often overlooked, but crucial. Using efficient serializers (like MessagePack or Protocol Buffers as alternatives), removing unnecessary fields, and implementing partial response patterns can significantly reduce payload size. I've seen API response sizes shrink by 60% through careful serialization optimization. 4. 𝗧𝗵𝗲 𝗡+𝟭 𝗤𝘂𝗲𝗿𝘆 𝗞𝗶𝗹𝗹𝗲𝗿 This is the silent performance killer in many APIs. Using eager loading, implementing GraphQL for flexible data fetching, or utilizing batch loading techniques (like DataLoader pattern) can transform your API's database interaction patterns. 5. 𝗖𝗼𝗺𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 𝗧𝗲𝗰𝗵𝗻𝗶𝗾𝘂𝗲𝘀 GZIP or Brotli compression isn't just about smaller payloads – it's about finding the right balance between CPU usage and transfer size. Modern compression algorithms can reduce payload size by up to 70% with minimal CPU overhead. 6. 𝗖𝗼𝗻𝗻𝗲𝗰𝘁𝗶𝗼𝗻 𝗣𝗼𝗼𝗹 A well-configured connection pool is your API's best friend. Whether it's database connections or HTTP clients, maintaining an optimal pool size based on your infrastructure capabilities can prevent connection bottlenecks and reduce latency spikes. 7. 𝗜𝗻𝘁𝗲𝗹𝗹𝗶𝗴𝗲𝗻𝘁 𝗟𝗼𝗮𝗱 𝗗𝗶𝘀𝘁𝗿𝗶𝗯𝘂𝘁𝗶𝗼𝗻 Beyond simple round-robin – implement adaptive load balancing that considers server health, current load, and geographical proximity. Tools like Kubernetes horizontal pod autoscaling can help automatically adjust resources based on real-time demand. In my experience, implementing these techniques reduces average response times from 800ms to under 100ms and helps handle 10x more traffic with the same infrastructure. Which of these techniques made the most significant impact on your API optimization journey?

  • Supercharge Your Model Training: Essential Techniques and Tricks 🚀 Are you tired of long model training times and inefficient training process? I have always struggled to understand which techniques can be chained together towards cumulative improvement and the order of magnitude improvement from each. Here is an array of powerful techniques to accelerate training with their effect size. The key in most cases is to know the memory architecture for the GPU  💾 and utilize it optimally by reducing data movement between on chip registers, cache, and off chip high-bandwidth memory. Frameworks like PyTorch make this pretty simple allowing you to do this in a few lines of code at most. - Switch to Mixed Precision: 🔢 Implementing bfloat16 can lead to a potential 3x speedup by reducing the amount of data transferred, thus enabling larger batch sizes. Although GPUs may promise up to an 8x improvement, actual gains could be lower due to memory constraints. Benchmarking is essential! - PyTorch Compile: 🖥️ Experience about a 2.5x speed increase by minimizing unnecessary memory bus traffic. This approach prepares your computations for more efficient execution. - Flash Attention: ⚡ Utilize a fused kernel specifically optimized for attention-heavy models, which can boost performance by up to 40% by enhancing memory hierarchy utilization. - Optimized Data Formats: 📊 Aligning your vocab size to a power of 2 can provide a straightforward 10% speed boost by improving memory access efficiency. - Hyperparameter Tuning: 🛠️ Gain an additional 5-10% speed by tweaking hyperparameters and employing fused kernels for optimizers like AdamW. Bespoke Fused Kernels: 🧩 Push the boundaries with custom kernels designed specifically for your model’s architecture to achieve optimal performance. Leverage Additional Optimizations: ➕ Employ vector operations (e.g., AVX-512) on CPUs or use sparse kernels for pruned models to further enhance memory efficiency. Scale Responsibly: 📈 Before moving to a multi-GPU setup, ensure you've maximized the potential of single-GPU optimizations to avoid inefficiencies. Once your setup is optimized, scaling across multiple GPUs can dramatically reduce training times by parallelizing the workload and minimizing data transfers. You can do this almost trivially by using things like Hugging Face Accelerate. Remember, the effectiveness of these techniques can vary based on your specific model, hardware setup, and other variables. Extensive benchmarking is crucial to find the perfect balance between speed and accuracy. Optimization is a continuous journey. Stay proactive in exploring new methods to reduce training times and remain competitive in the fast-evolving field of machine learning. For more insights, check out Karpathy’s latest video where he replicates GPT-2 on 8x A100s, astonishingly beating GPT-3 on Hellaswag. It’s incredible to see such advancements, allowing what once took months to be accomplished virtually overnight. 🌙✨

  • I found one small function in Cleartrip’s sourcemap that quietly fixes a performance problem most frontend teams don’t even know they’re causing.  requestIdleCallback(() => {       cb()     }) (see the screenshot) At first glance, it looks basic. But the idea behind it is extremely powerful. The real bottleneck in modern frontends isn’t React, Vue, or whatever framework you love blaming. It’s bad task scheduling.Code that works, but runs at the wrong time. This fixes that without touching a single line of business logic. What it actually achieves,It forces all non-urgent work to wait until the browser is genuinely free. Not kind of free or may beFree. requestIdleCallback: → Browser idle? Run the task. Fallback setTimeout(..., 0): → Push it out of the current frame so it doesn’t fight the UI. Result: UI stays focused on rendering, scrolling, input, and paint. Everything else waits its turn like a good citizen. Where this matters the most You throw these into idle: • analytics • logs • caching writes • JSON parsing • data massaging • preloading future screens • any important but not right now job These don’t belong next to your render cycle . Frontend performance isn’t optimize code. It’s run the right code at the right time.

  • View profile for Sahil Chopra

    AI Web Engineer | Educator | Code Enthusiast

    43,799 followers

    As a Frontend developer it is also important to prioritize performance optimization to ensure the web applications load quickly and provide a smooth user experience. Here's a breakdown of key techniques used for frontend performance optimization: Minification and Compression: Minification involves removing unnecessary characters (such as whitespace, comments, and unused code) from source files to reduce file size. Compression techniques like gzip or Brotli further reduce file sizes by compressing text-based resources like HTML, CSS, and JavaScript before transmitting them over the network. Smaller file sizes lead to faster download times and improved page loading speed. Image Optimization: Images often contribute significantly to page weight and load times. Optimizing images by compressing them without sacrificing quality, using appropriate image formats (such as WebP or JPEG XR), and implementing responsive image techniques (like srcset and sizes attributes) can dramatically improve performance. Additionally, lazy loading techniques delay the loading of off-screen images until they are needed, reducing initial page load times. Caching Strategies: Implementing caching strategies like browser caching, CDN caching, and server-side caching can reduce server load and speed up subsequent page loads. Leveraging HTTP caching headers such as Cache-Control and Expires allows browsers and intermediaries to store and reuse previously fetched resources, minimizing network requests. Asynchronous Loading: Loading JavaScript and CSS files asynchronously prevents them from blocking the rendering of the page, allowing critical content to display faster. Techniques like defer and async attributes for script tags and media attributes for stylesheet links enable asynchronous loading while ensuring proper execution order and avoiding render-blocking behavior. Code Splitting and Bundle Optimization: Code splitting involves breaking down large bundles of JavaScript or CSS code into smaller, more manageable chunks that can be loaded on-demand. Tools like Webpack offer features for code splitting, tree shaking (removing unused code), and optimizing bundle size, helping reduce initial load times and improve runtime performance. Critical Path Optimization: Identifying and optimizing the critical rendering path, which includes the resources necessary to render the initial view of a webpage, is crucial for improving perceived performance. Prioritizing the loading of critical resources (such as CSS and JavaScript required for above-the-fold content) and deferring non-essential resources can accelerate the time to first meaningful paint and enhance user perception of speed. #frontenddevelopment #performanceoptimization #webdevelopment #javascript

  • View profile for Ayman Anaam

    Dynamic Technology Leader | Innovator in .NET Development and Cloud Solutions

    11,614 followers

    Async/Await Mistakes That Are Killing Your API Performance Writing async code in C# and ASP.NET Core seems easy, but subtle mistakes can silently tank your app’s performance. Here are 8 mistakes that quietly destroy throughput, increase latency, and frustrate users: 🔹 1. The ConfigureAwait(false) Myth In ASP.NET Core, ConfigureAwait(false) is usually unnecessary. There's no SynchronizationContext, so you gain nothing and add clutter. Use it in libraries - not APIs. 🔹 2. Sync-over-Async: The Thread Pool Killer Calling .Result or .Wait() blocks threads. That’s death for scalability and a shortcut to deadlocks. 🔹 3. async void: The Silent Crasher Only use async void in event handlers. Anywhere else, exceptions go uncaught - and your app can crash without a trace. 🔹 4. Sequential Awaits in Loops Awaiting in a loop = slow. Use Task.WhenAll() to parallelize async operations and boost performance. 🔹 5. ValueTask Misuse ValueTask reduces allocations only when results are often sync (like cache). Otherwise, prefer Task to avoid overhead. 🔹 6. Async in Constructors Constructors can't be async. Blocking with .Result hurts startup time. Defer async initialization until needed. 🔹 7. Exceptions for Flow Control Catching exceptions is expensive. Don’t use try/catch for expected conditions - use guard clauses and result models instead. 🔹 8. Misusing Task.Run in APIs You're already on a thread pool. Wrapping logic in Task.Run just adds overhead - use async all the way for I/O. 🎯 Pro Tip: Profile Everything Performance bugs in async code are invisible until they blow up. Use tools like: ▪️ Application Insights ▪️ BenchmarkDotNet ▪️ PerfView ▪️ Custom metrics ✅ Key Takeaways • Avoid sync-over-async • Use Task.WhenAll for parallelism • Reserve ValueTask for hot paths • Handle expected failures without try/catch • Never assume async = fast - measure it Writing fast, scalable async code is a skill. Master it — and your API will thank you. 👉 Which of these mistakes have you seen in real projects? #dotnet #aspnetcore #csharp #performance #asyncawait #programmingtips #webapi #devtips

  • View profile for Paige Bailey

    ✨ Radically improving the developer experience for generative AI.

    44,314 followers

    Just tested a Claude code performance profiling prompt (see below) with the Gemini 1.5 model and an example matplotlib animation (https://lnkd.in/grhJudpD). It'd be so useful to have this feature baked into an IDE extension, or version control tooling! 😁 ======== Prompt: <prompt_explanation> You are a world expert in making code run faster. You use any resource you can to do so. Given some code, first, explain how the code works to me. Next, for each part of the code, identify how long it might take to run. After that, identify which parts are key candidates to speed up. Then, make a table with axes 'Impact' and 'Complexity'. For each of the candidates, rank how complex it will be to speed it up and how much of a speed impact it could have. After that, order the candidates by ranking. Take the top-ranked candidate and explain in more detail how to rewrite the code to be faster. Then, rewrite the actual code. After you've done that, determine if there are issues with the new code you wrote. If so, move on. Otherwise, rewrite the code again to fix them. Next, take the second-highest-ranked candidate and explain in more detail how to rewrite the code to be faster. Then, rewrite the actual code. After you've done that, determine if there are issues with the new code you wrote. If so, move on. Otherwise, rewrite the code again to fix them. Then do the same for the third-highest-ranked candidate. Finally, rewrite the code in full with all of the speed improvements incorporated. </prompt_explanation> Here is the format you should respond in: <response_format> ## Explanation: $explanation ## Runtime Analysis: $runtime_analysis ## Key Candidates for Speed Up: $candidates ## Impact and Complexity Table: | Candidate | Impact | Complexity | | --------- | ------ | ---------- | $candidate_table ## Candidates Ordered by Ranking: $ordered_candidates ## Detailed Explanation and Code Rewrite for Top Candidate: # Explanation $top_candidate_explanation # Code Rewrite $top_candidate_code # Issues with New Code: *(include this section only if they exist)* $top_candidate_issues # Code Rewrite, Try 2: *(include this section only if issues exist)* $top_candidate_code_try2 ## Detailed Explanation and Code Rewrite for Second-Highest Candidate: # Explanation $second_candidate_explanation # Code Rewrite $second_candidate_code # Issues with New Code: *(include this section only if issues exist)* $second_candidate_issues # Code Rewrite, Try 2: *(include this section only if issues exist)* $second_candidate_code_try2 ## Detailed Explanation and Code Rewrite for Third-Highest Candidate: # Explanation $third_candidate_explanation # Code Rewrite $third_candidate_code # Issues with New Code: *(include this section only if issues exist)* $third_candidate_issues # Code Rewrite, Try 2: *(include this section only if issues exist)* $third_candidate_code_try2 ## Full Code Rewrite with Speed Improvements: $full_code_rewrite </response_format> ---

  • View profile for Janhavi Patil

    Data & AI Engineer | Building Enterprise Data Platforms, AI Applications & Real-Time Analytics | SQL • Java • Python • Snowflake

    6,864 followers

    With a background in data engineering and business analysis, I’ve consistently seen the immense impact of optimized SQL code on improving the performance and efficiency of database operations. It indirectly contributes to cost savings by reducing resource consumption. Here are some techniques that have proven invaluable in my experience: 1. Index Large Tables: Indexing tables with large datasets (>1,000,000 rows) greatly speeds up searches and enhances query performance. However, be cautious of over-indexing, as excessive indexes can degrade write operations. 2. Select Specific Fields: Choosing specific fields instead of using SELECT * reduces the amount of data transferred and processed, which improves speed and efficiency. 3. Replace Subqueries with Joins: Using joins instead of subqueries in the WHERE clause can improve performance. 4. Use UNION ALL Instead of UNION: UNION ALL is preferable over UNION because it does not involve the overhead of sorting and removing duplicates. 5. Optimize with WHERE Instead of HAVING: Filtering data with WHERE clauses before aggregation operations reduces the workload and speeds up query processing. 6. Utilize INNER JOIN Instead of WHERE for Joins: INNER JOINs help the query optimizer make better execution decisions than complex WHERE conditions. 7. Minimize Use of OR in Joins: Avoiding the OR operator in joins enhances performance by simplifying the conditions and potentially reducing the dataset earlier in the execution process. 8. Use Views: Creating views instead of results that can be accessed faster than recalculating the views each time they are needed. 9. Minimize the Number of Subqueries: Reducing the number of subqueries in your SQL statements can significantly enhance performance by decreasing the complexity of the query execution plan and reducing overhead. 10. Implement Partitioning: Partitioning large tables can improve query performance and manageability by logically dividing them into discrete segments. This allows SQL queries to process only the relevant portions of data. #SQL #DataOptimization #DatabaseManagement #PerformanceTuning #DataEngineering

  • View profile for Milan Jovanović
    Milan Jovanović Milan Jovanović is an Influencer

    Practical .NET and Software Architecture Tips | Microsoft MVP

    279,489 followers

    I spent 17 hours optimizing an API endpoint to make it 15x faster. Here's a breakdown of what I did. I worked on an e-commerce application. One endpoint was crunching some heavy numbers. And it wasn't scaling well. The endpoint calculated a report. It needed data from several services to perform the calculations. This is the high-level process I took: - Identify the bottlenecks - Fix the database queries - Fix the external API calls - Add caching as a final touch 𝗦𝗼, 𝗵𝗼𝘄 𝗱𝗼 𝘆𝗼𝘂 𝗶𝗱𝗲𝗻𝘁𝗶𝗳𝘆 𝘁𝗵𝗲 𝗯𝗼𝘁𝘁𝗹𝗲𝗻𝗲𝗰𝗸𝘀 𝗶𝗻 𝘆𝗼𝘂𝗿 𝘀𝘆𝘀𝘁𝗲𝗺? If you know the slowest piece of code, you will know what to fix. The 80/20 rule works wonders here. Improving 20% of the slowest code can yield an 80% improvement. The fun doesn't stop here. Performance optimization is a continuous process and requires constant monitoring and improvements. Fixing one problem will reveal the next one. The problems I found were: - Calling the database from a loop - Calling an external service many times - Duplicate calculations with the same parameters Measuring performance is also a crucial step in the optimization process: - Logging execution times with a Timer/Stopwatch - If you have detailed application metrics, even better - Use a performance profiler tool to find slow code 𝗙𝗶𝘅𝗶𝗻𝗴 𝘀𝗹𝗼𝘄 𝗱𝗮𝘁𝗮𝗯𝗮𝘀𝗲 𝗾𝘂𝗲𝗿𝗶𝗲𝘀 A round trip between your application and a database or service can last 5-10ms (or more). The more round trips you have, the more it adds up. Here are a few things you can do to improve this: - Don't call the database from a loop - Return multiple results in one query 𝗖𝗼𝗻𝗰𝘂𝗿𝗿𝗲𝗻𝘁 𝗲��𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝗶𝘀 𝘆𝗼𝘂𝗿 𝗳𝗿𝗶𝗲𝗻𝗱 I had multiple asynchronous calls to different services. These services were independent of each other. So, I called these services concurrently and aggregated the results. This simple technique helped me achieve significant performance improvement. 𝗖𝗮𝗰𝗵𝗶𝗻𝗴 𝗮𝘀 𝗮 𝗹𝗮𝘀𝘁 𝗿𝗲𝘀𝗼𝗿𝘁 Caching is an effective way to speed up an application. But it can introduce bugs when the data is stale. Is this tradeoff worth it? In my case, achieving the desired performance was critical. You also have to consider the cache expiration and eviction strategies. A few caching options in ASP .NET: - IMemoryCache (uses server RAM) - IDistributedCache (Redis, Azure Cache for Redis) What do you think of my process? Would you do something differently? --- Subscribe to my weekly newsletter to accelerate your .NET skills: https://bit.ly/3R9JnT5

  • View profile for Pooya Eimandar

    Stay Hungry Stay Foolish

    5,764 followers

    A paper released last year by Bilokon and one of his PhD students, Burak Gunduz, looks at 12 techniques for reducing latency in C++ code, as follows: 🚀 Lock-free programming: A concurrent programming paradigm involving multi-threaded algorithms which, unlike their traditional counterparts, do not employ the usage of mutual exclusion mechanisms, such as locks, to arbitrate access to shared resources. 🚀 SIMD instructions: Instructions that take advantage of the parallel processing power of contemporary CPUs, allowing the simultaneous execution of multiple operations. 🚀 Mixing data types: When a computation involves both float and double types, implicit conversions are required. If only float computations are used, performance improves. 🚀 Signed vs unsigned: Ensuring consistent signedness in comparisons to avoid conversions. 🚀 Prefetching: Explicitly loading data into cache before it is needed to reduce data fetch delays, particularly in memory-bound applications. 🚀 Branch reduction: Predicting conditional branch outcomes to allow speculative code execution. 🚀 Slowpath removal: Minimizing the execution of rarely executed code paths. 🚀 Short-circuiting: Logical expressions cease evaluation when the final result is determined. 🚀 Inlining: Incorporating the body of a function at each point the function is called, reducing function call overhead and enabling further optimization by the compiler. 🚀 Constexpr: Computations marked as constexpr are evaluated at compile time, enabling constant folding and efficient code execution by eliminating runtime calculations. 🚀 Compile-time dispatch: Techniques like template specialization or function overloading that ensure optimized code paths are chosen at compile time based on type or value, avoiding runtime dispatch and enabling early optimization decisions. 🚀 Cache warming: To minimize memory access time and boost program responsiveness, data is preloaded into the CPU cache before it’s needed. Reference: https://lnkd.in/dDfYJyw6 #technology #tech #cpp #programming

  • View profile for Rocky Bhatia

    400K+ Engineers | Architect @ Adobe | GenAI & Systems at Scale

    217,141 followers

    Boosting API Performance: Best Practices and Techniques Improving API performance often involves a combination of strategies and techniques. Here are some methods to enhance API performance, focusing on pagination, asynchronous logging, connection pooling, caching, load balancing, and payload compression: 1. Pagination:   Implement server-side pagination to limit the amount of data transferred in a single request/response. Allow clients to request a specific page or range of data.   Use query parameters like `page` and `pageSize` to control the pagination, and ensure your API documentation is clear on how to use it. 2. Asynchronous Logging:   Log asynchronously to avoid introducing latency to API responses. Use a message queue or a dedicated logging service to process logs in the background.   This decouples the logging process from the request/response cycle, improving API responsiveness. 3. Connection Pooling:   Use connection pooling for database and other resource intensive operations. Connection pooling helps efficiently manage and reuse database connections, reducing overhead. 4. Caching:   Implement caching mechanisms to store frequently requested data. Consider using in memory caching systems like Redis or Memcached to speed up data retrieval.   Utilise HTTP caching headers (e.g., `CacheControl`, `ETag`) to instruct clients and intermediaries to cache responses, reducing the load on your API. 5. Load Balancing:   Set up load balancers to distribute incoming traffic across multiple API servers or instances. This ensures even load distribution and redundancy.   Consider using dynamic load balancing algorithms to adapt to changing server loads. 6. Payload Compression:   Compress API responses before sending them to clients. Use popular compression algorithms like GZIP, Brotli, or Zstandard to reduce data transfer times.   Ensure that clients support decompression of compressed payloads. Remember that the effectiveness of these methods depends on the specific requirements of your API and the technologies you are using. Monitoring and performance testing are crucial to fine tune and optimise your API further. Additionally, consider using content delivery networks (CDNs) to distribute static content, and use API gateways to manage and secure your API endpoints effectively.

Explore categories