{"title":"Penampilan dan optimalisasi","version":"6.0","locale":"id","docname":"topics/performance","url":"/id/6.0/topics/performance/","canonical":"https://djangodocs.dev/id/6.0/topics/performance/","summary":"This document provides an overview of techniques and tools that can help get your Django code running more efficiently - faster, and using fewer system resources.…","html":"<h1>Penampilan dan optimalisasi<a class=\"heading-anchor\" href=\"#performance-and-optimization\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document provides an overview of techniques and tools that can help get\nyour Django code running more efficiently - faster, and using fewer system\nresources.</p>\n<section id=\"introduction\">\n<h2>Kata Pengantar<a class=\"heading-anchor\" href=\"#introduction\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Generally one's first concern is to write code that <em>works</em>, whose logic\nfunctions as required to produce the expected output. Sometimes, however, this\nwill not be enough to make the code work as <em>efficiently</em> as one would like.</p>\n<p>In this case, what's needed is something - and in practice, often a collection\nof things - to improve the code's performance without, or only minimally,\naffecting its behavior.</p>\n</section>\n<section id=\"general-approaches\">\n<h2>Pendekatan umum<a class=\"heading-anchor\" href=\"#general-approaches\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"what-are-you-optimizing-for\">\n<h3>What are you optimizing <em>for</em>?<a class=\"heading-anchor\" href=\"#what-are-you-optimizing-for\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>It's important to have a clear idea what you mean by 'performance'. There is\nnot just one metric of it.</p>\n<p>Improved speed might be the most obvious aim for a program, but sometimes other\nperformance improvements might be sought, such as lower memory consumption or\nfewer demands on the database or network.</p>\n<p>Improvements in one area will often bring about improved performance in\nanother, but not always; sometimes one can even be at the expense of another.\nFor example, an improvement in a program's speed might cause it to use more\nmemory. Even worse, it can be self-defeating - if the speed improvement is so\nmemory-hungry that the system starts to run out of memory, you'll have done\nmore harm than good.</p>\n<p>There are other trade-offs to bear in mind. Your own time is a valuable\nresource, more precious than CPU time. Some improvements might be too difficult\nto be worth implementing, or might affect the portability or maintainability of\nthe code. Not all performance improvements are worth the effort.</p>\n<p>So, you need to know what performance improvements you are aiming for, and you\nalso need to know that you have a good reason for aiming in that direction -\nand for that you need:</p>\n</section>\n<section id=\"performance-benchmarking\">\n<h3>Penampilan pembandingan<a class=\"heading-anchor\" href=\"#performance-benchmarking\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>It's no good just guessing or assuming where the inefficiencies lie in your\ncode.</p>\n<section id=\"django-tools\">\n<h4>Alat-alat Django<a class=\"heading-anchor\" href=\"#django-tools\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"extlink-pypi reference external\" href=\"https://pypi.org/project/django-debug-toolbar/\">django-debug-toolbar</a> is a very handy tool that provides insights into\nwhat your code is doing and how much time it spends doing it. In particular it\ncan show you all the SQL queries your page is generating, and how long each one\nhas taken.</p>\n<p>Third-party panels are also available for the toolbar, that can (for example)\nreport on cache performance and template rendering times.</p>\n</section>\n<section id=\"third-party-services\">\n<h4>Layanan pihak-ketiga<a class=\"heading-anchor\" href=\"#third-party-services\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>There are a number of free services that will analyze and report on the\nperformance of your site's pages from the perspective of a remote HTTP client,\nin effect simulating the experience of an actual user.</p>\n<p>These can't report on the internals of your code, but can provide a useful\ninsight into your site's overall performance, including aspects that can't be\nadequately measured from within Django environment.</p>\n<p>There are also several paid-for services that perform a similar analysis,\nincluding some that are Django-aware and can integrate with your codebase to\nprofile its performance far more comprehensively.</p>\n</section>\n</section>\n<section id=\"get-things-right-from-the-start\">\n<h3>Mendapatkan hal yang benar dari awal<a class=\"heading-anchor\" href=\"#get-things-right-from-the-start\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Some work in optimization involves tackling performance shortcomings, but some\nof the work can be built-in to what you'd do anyway, as part of the good\npractices you should adopt even before you start thinking about improving\nperformance.</p>\n<p>In this respect Python is an excellent language to work with, because solutions\nthat look elegant and feel right usually are the best performing ones. As with\nmost skills, learning what &quot;looks right&quot; takes practice, but one of the most\nuseful guidelines is:</p>\n<section id=\"work-at-the-appropriate-level\">\n<h4>Bekerja pada tingkatan sesuai<a class=\"heading-anchor\" href=\"#work-at-the-appropriate-level\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Django offers many different ways of approaching things, but just because it's\npossible to do something in a certain way doesn't mean that it's the most\nappropriate way to do it. For example, you might find that you could calculate\nthe same thing - the number of items in a collection, perhaps - in a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code>, in Python, or in a template.</p>\n<p>However, it will almost always be faster to do this work at lower rather than\nhigher levels. At higher levels the system has to deal with objects through\nmultiple levels of abstraction and layers of machinery.</p>\n<p>That is, the database can typically do things faster than Python can, which can\ndo them faster than the template language can:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># QuerySet operation on the database</span>\n<span class=\"c1\"># fast, because that&#39;s what databases are good at</span>\n<span class=\"n\">my_bicycles</span><span class=\"o\">.</span><span class=\"n\">count</span><span class=\"p\">()</span>\n\n<span class=\"c1\"># counting Python objects</span>\n<span class=\"c1\"># slower, because it requires a database query anyway, and processing</span>\n<span class=\"c1\"># of the Python objects</span>\n<span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">my_bicycles</span><span class=\"p\">)</span>\n</code></pre></div>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cm\">&lt;!--</span>\n<span class=\"cm\">Django template filter</span>\n<span class=\"cm\">slower still, because it will have to count them in Python anyway,</span>\n<span class=\"cm\">and because of template language overheads</span>\n<span class=\"cm\">--&gt;</span>\n<span class=\"cp\">{{</span> <span class=\"nv\">my_bicycles</span><span class=\"o\">|</span><span class=\"nf\">length</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>Generally speaking, the most appropriate level for the job is the lowest-level\none that it is comfortable to code for.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>Contoh di atas hanyalah ilustrasi.</p>\n<p>Firstly, in a real-life case you need to consider what is happening before\nand after your count to work out what's an optimal way of doing it <em>in that\nparticular context</em>. The database optimization document describes <a class=\"reference internal\" href=\"/id/6.0/topics/db/optimization/#overuse-of-count-and-exists\"><span class=\"std std-ref\">a\ncase where counting in the template would be better</span></a>.</p>\n<p>Secondly, there are other options to consider: in a real-life case, <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span>\n<span class=\"pre\">my_bicycles.count</span> <span class=\"pre\">}}</span></code>, which invokes the <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> <code class=\"docutils literal notranslate\"><span class=\"pre\">count()</span></code> method\ndirectly from the template, might be the most appropriate choice.</p>\n</aside>\n</section>\n</section>\n</section>\n<section id=\"caching\">\n<h2>Menembolok<a class=\"heading-anchor\" href=\"#caching\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Often it is expensive (that is, resource-hungry and slow) to compute a value,\nso there can be huge benefit in saving the value to a quickly accessible cache,\nready for the next time it's required.</p>\n<p>It's a sufficiently significant and powerful technique that Django includes a\ncomprehensive caching framework, as well as other smaller pieces of caching\nfunctionality.</p>\n<section id=\"the-caching-framework\">\n<h3><span class=\"xref std std-doc\">The caching framework 1</span><a class=\"heading-anchor\" href=\"#the-caching-framework\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django's <a class=\"reference internal\" href=\"/id/6.0/topics/cache/\"><span class=\"doc\">caching framework</span></a> offers very significant\nopportunities for performance gains, by saving dynamic content so that it\ndoesn't need to be calculated for each request.</p>\n<p>For convenience, Django offers different levels of cache granularity: you can\ncache the output of specific views, or only the pieces that are difficult to\nproduce, or even an entire site.</p>\n<p>Implementing caching should not be regarded as an alternative to improving code\nthat's performing poorly because it has been written badly. It's one of the\nfinal steps toward producing well-performing code, not a shortcut.</p>\n</section>\n<section id=\"cached-property\">\n<h3><a class=\"reference internal\" href=\"/id/6.0/ref/utils/#django.utils.functional.cached_property\" title=\"django.utils.functional.cached_property\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">cached_property</span></code></a><a class=\"heading-anchor\" href=\"#cached-property\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>It's common to have to call a class instance's method more than once. If\nthat function is expensive, then doing so can be wasteful.</p>\n<p>Using the <a class=\"reference internal\" href=\"/id/6.0/ref/utils/#django.utils.functional.cached_property\" title=\"django.utils.functional.cached_property\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">cached_property</span></code></a> decorator saves the\nvalue returned by a property; the next time the function is called on that\ninstance, it will return the saved value rather than re-computing it. Note that\nthis only works on methods that take <code class=\"docutils literal notranslate\"><span class=\"pre\">self</span></code> as their only argument and that\nit changes the method to a property.</p>\n<p>Certain Django components also have their own caching functionality; these are\ndiscussed below in the sections related to those components.</p>\n</section>\n</section>\n<section id=\"understanding-laziness\">\n<h2>Memahami laziness<a class=\"heading-anchor\" href=\"#understanding-laziness\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><em>Laziness</em> is a strategy complementary to caching. Caching avoids\nrecomputation by saving results; laziness delays computation until it's\nactually required.</p>\n<p>Laziness allows us to refer to things before they are instantiated, or even\nbefore it's possible to instantiate them. This has numerous uses.</p>\n<p>For example, <a class=\"reference internal\" href=\"/id/6.0/topics/i18n/translation/#lazy-translations\"><span class=\"std std-ref\">lazy translation</span></a> can be used before the\ntarget language is even known, because it doesn't take place until the\ntranslated string is actually required, such as in a rendered template.</p>\n<p>Laziness is also a way to save effort by trying to avoid work in the first\nplace. That is, one aspect of laziness is not doing anything until it has to be\ndone, because it may not turn out to be necessary after all. Laziness can\ntherefore have performance implications, and the more expensive the work\nconcerned, the more there is to gain through laziness.</p>\n<p>Python provides a number of tools for lazy evaluation, particularly through the\n<a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-generator\" title=\"(di Python v3.14)\"><span class=\"xref std std-term\">generator</span></a> and <a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-generator-expression\" title=\"(di Python v3.14)\"><span class=\"xref std std-term\">generator expression</span></a> constructs. It's worth\nreading up on laziness in Python to discover opportunities for making use of\nlazy patterns in your code.</p>\n<section id=\"laziness-in-django\">\n<h3>Laziness di Django<a class=\"heading-anchor\" href=\"#laziness-in-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django is itself quite lazy. A good example of this can be found in the\nevaluation of a <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code>. <a class=\"reference internal\" href=\"/id/6.0/topics/db/queries/#querysets-are-lazy\"><span class=\"std std-ref\">QuerySets are lazy</span></a>.\nThus a <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> can be created, passed around and combined with other\n<code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> instances, without actually incurring any trips to the database\nto fetch the items it describes. What gets passed around is the <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code>\nobject, not the collection of items that - eventually - will be required from\nthe database.</p>\n<p>Di sisi lain, t:ref:certain operations will force the evaluation of a QuerySet 1. Mengindari penilaian sebelum waktunya dri <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> dapat menyimpan membuat perjalanan mahal dan tidak diperlukan pada basisdata.</p>\n<p>Django juga menawarkan penghias <a class=\"reference internal\" href=\"/id/6.0/ref/utils/#django.utils.functional.keep_lazy\" title=\"django.utils.functional.keep_lazy\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">keep_lazy()</span></code></a>. Ini mengizinkan sebuah fungsi yang telah dipanggil dengan argumen lazy untuk berperilaku malas itu sendiri, hanya sedang dinilai ketika itu dibutuhkan. Dengan demikian argumen lazy - yang dapat sangat mahal -  tidak akan dipanggil untuk penilaian sampai itu semata-mata diperlukan.</p>\n</section>\n</section>\n<section id=\"databases\">\n<h2>Basisdata<a class=\"heading-anchor\" href=\"#databases\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"database-optimization\">\n<h3>Optimasi basisdata<a class=\"heading-anchor\" href=\"#database-optimization\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django's database layer provides various ways to help developers get the best\nperformance from their databases. The <a class=\"reference internal\" href=\"/id/6.0/topics/db/optimization/\"><span class=\"doc\">database optimization documentation</span></a> gathers together links to the relevant\ndocumentation and adds various tips that outline the steps to take when\nattempting to optimize your database usage.</p>\n</section>\n<section id=\"other-database-related-tips\">\n<h3>Other database-related tips<a class=\"heading-anchor\" href=\"#other-database-related-tips\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Enabling <a class=\"reference internal\" href=\"/id/6.0/ref/databases/#persistent-database-connections\"><span class=\"std std-ref\">Hubungan tetap</span></a> can speed up connections to the\ndatabase accounts for a significant part of the request processing time.</p>\n<p>This helps a lot on virtualized hosts with limited network performance, for\nexample.</p>\n</section>\n</section>\n<section id=\"http-performance\">\n<h2>Penampilan HTTP<a class=\"heading-anchor\" href=\"#http-performance\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"middleware\">\n<h3>Middleware<a class=\"heading-anchor\" href=\"#middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django comes with a few helpful pieces of <a class=\"reference internal\" href=\"/id/6.0/ref/middleware/\"><span class=\"doc\">middleware</span></a>\nthat can help optimize your site's performance. They include:</p>\n<section id=\"conditionalgetmiddleware\">\n<h4><a class=\"reference internal\" href=\"/id/6.0/ref/middleware/#django.middleware.http.ConditionalGetMiddleware\" title=\"django.middleware.http.ConditionalGetMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ConditionalGetMiddleware</span></code></a><a class=\"heading-anchor\" href=\"#conditionalgetmiddleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Adds support for modern browsers to conditionally GET responses based on the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">Last-Modified</span></code> headers. It also calculates and sets an ETag if\nneeded.</p>\n</section>\n<section id=\"gzipmiddleware\">\n<h4><a class=\"reference internal\" href=\"/id/6.0/ref/middleware/#django.middleware.gzip.GZipMiddleware\" title=\"django.middleware.gzip.GZipMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code></a><a class=\"heading-anchor\" href=\"#gzipmiddleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Compresses responses for all modern browsers, saving bandwidth and transfer\ntime. Note that GZipMiddleware is currently considered a security risk, and is\nvulnerable to attacks that nullify the protection provided by TLS/SSL. See the\nwarning in <a class=\"reference internal\" href=\"/id/6.0/ref/middleware/#django.middleware.gzip.GZipMiddleware\" title=\"django.middleware.gzip.GZipMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code></a> for more\ninformation.</p>\n</section>\n</section>\n<section id=\"sessions\">\n<h3>Sesi<a class=\"heading-anchor\" href=\"#sessions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"using-cached-sessions\">\n<h4>Menggunakan sesi tembolok<a class=\"heading-anchor\" href=\"#using-cached-sessions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference internal\" href=\"/id/6.0/topics/http/sessions/#cached-sessions-backend\"><span class=\"std std-ref\">Using cached sessions</span></a> may be a way to increase\nperformance by eliminating the need to load session data from a slower storage\nsource like the database and instead storing frequently used session data in\nmemory.</p>\n</section>\n</section>\n<section id=\"static-files\">\n<h3>Berkas statis<a class=\"heading-anchor\" href=\"#static-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Static files, which by definition are not dynamic, make an excellent target for\noptimization gains.</p>\n<section id=\"manifeststaticfilesstorage\">\n<h4><a class=\"reference internal\" href=\"/id/6.0/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage\" title=\"django.contrib.staticfiles.storage.ManifestStaticFilesStorage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ManifestStaticFilesStorage</span></code></a><a class=\"heading-anchor\" href=\"#manifeststaticfilesstorage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>By taking advantage of web browsers' caching abilities, you can\neliminate network hits entirely for a given file after the initial download.</p>\n<p><a class=\"reference internal\" href=\"/id/6.0/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage\" title=\"django.contrib.staticfiles.storage.ManifestStaticFilesStorage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ManifestStaticFilesStorage</span></code></a> appends\na content-dependent tag to the filenames of <a class=\"reference internal\" href=\"/id/6.0/ref/contrib/staticfiles/\"><span class=\"doc\">static files</span></a> to make it safe for browsers to cache them\nlong-term without missing future changes - when a file changes, so will the\ntag, so browsers will reload the asset automatically.</p>\n</section>\n<section id=\"minification\">\n<h4>&quot;Minifikasi&quot;<a class=\"heading-anchor\" href=\"#minification\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Beberapa alat-alat pihak-ketiga Django dan peket-paket menyediakan kemampuan untuk &quot;minify&quot; HTML, CSS, dan JavaScript. Mereka memindahkan ruang kosong yang tidak diperlukan, baris baru, dan komentar, dan memperpendek nama-nama variabel, dan mengurangi ukuran dari dokumen-dokumen yang situs anda terbitkan.</p>\n</section>\n</section>\n</section>\n<section id=\"template-performance\">\n<h2>Penampilan cetakan<a class=\"heading-anchor\" href=\"#template-performance\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Catat bahwa:</p>\n<ul class=\"simple\">\n<li><p>menggunakan <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">block</span> <span class=\"pre\">%}</span></code> lebih cepat daripada menggunakan <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">include</span> <span class=\"pre\">%}</span></code></p></li>\n<li><p>heavily-fragmented templates, assembled from many small pieces, can affect\nperformance</p></li>\n</ul>\n<section id=\"the-cached-template-loader\">\n<h3>The cached template loader<a class=\"heading-anchor\" href=\"#the-cached-template-loader\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Enabling the <a class=\"reference internal\" href=\"/id/6.0/ref/templates/api/#django.template.loaders.cached.Loader\" title=\"django.template.loaders.cached.Loader\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">cached</span> <span class=\"pre\">template</span> <span class=\"pre\">loader</span></code></a> often improves performance\ndrastically, as it avoids compiling each template every time it needs to be\nrendered.</p>\n</section>\n</section>\n<section id=\"using-different-versions-of-available-software\">\n<h2>Menggunakan versi berbeda dari perangkat lunak tersedia<a class=\"heading-anchor\" href=\"#using-different-versions-of-available-software\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>It can sometimes be worth checking whether different and better-performing\nversions of the software that you're using are available.</p>\n<p>These techniques are targeted at more advanced users who want to push the\nboundaries of performance of an already well-optimized Django site.</p>\n<p>However, they are not magic solutions to performance problems, and they're\nunlikely to bring better than marginal gains to sites that don't already do the\nmore basic things the right way.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>It's worth repeating: <strong>reaching for alternatives to software you're\nalready using is never the first answer to performance problems</strong>. When\nyou reach this level of optimization, you need a formal benchmarking\nsolution.</p>\n</aside>\n<section id=\"newer-is-often-but-not-always-better\">\n<h3>Terbaru adalah sering - tetapi tidak selalu - terbaik<a class=\"heading-anchor\" href=\"#newer-is-often-but-not-always-better\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>It's fairly rare for a new release of well-maintained software to be less\nefficient, but the maintainers can't anticipate every possible use-case - so\nwhile being aware that newer versions are likely to perform better, don't\nassume that they always will.</p>\n<p>This is true of Django itself. Successive releases have offered a number of\nimprovements across the system, but you should still check the real-world\nperformance of your application, because in some cases you may find that\nchanges mean it performs worse rather than better.</p>\n<p>Newer versions of Python, and also of Python packages, will often perform\nbetter too - but measure, rather than assume.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>Unless you've encountered an unusual performance problem in a particular\nversion, you'll generally find better features, reliability, and security\nin a new release and that these benefits are far more significant than any\nperformance you might win or lose.</p>\n</aside>\n</section>\n<section id=\"alternatives-to-django-s-template-language\">\n<h3>Jalan lain ke bahasa cetakan Django<a class=\"heading-anchor\" href=\"#alternatives-to-django-s-template-language\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>For nearly all cases, Django's built-in template language is perfectly\nadequate. However, if the bottlenecks in your Django project seem to lie in the\ntemplate system and you have exhausted other opportunities to remedy this, a\nthird-party alternative may be the answer.</p>\n<p><a class=\"reference external\" href=\"https://jinja.palletsprojects.com/\">Jinja2</a> can offer performance improvements, particularly when it comes to\nspeed.</p>\n<p>Alternative template systems vary in the extent to which they share Django's\ntemplating language.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p><em>If</em> you experience performance issues in templates, the first thing to do\nis to understand exactly why. Using an alternative template system may\nprove faster, but the same gains may also be available without going to\nthat trouble - for example, expensive processing and logic in your\ntemplates could be done more efficiently in your views.</p>\n</aside>\n</section>\n<section id=\"alternative-software-implementations\">\n<h3>Jalan lain penerapan perangkat lunak<a class=\"heading-anchor\" href=\"#alternative-software-implementations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>It may be worth checking whether Python software you're using has been\nprovided in a different implementation that can execute the same code faster.</p>\n<p>However: most performance problems in well-written Django sites aren't at the\nPython execution level, but rather in inefficient database querying, caching,\nand templates. If you're relying on poorly-written Python code, your\nperformance problems are unlikely to be solved by having it execute faster.</p>\n<p>Using an alternative implementation may introduce compatibility, deployment,\nportability, or maintenance issues. It goes without saying that before adopting\na non-standard implementation you should ensure it provides sufficient\nperformance gains for your application to outweigh the potential risks.</p>\n<p>With these caveats in mind, you should be aware of:</p>\n<section id=\"id1\">\n<h4><a class=\"reference external\" href=\"https://www.pypy.org/\">PyPy</a><a class=\"heading-anchor\" href=\"#id1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference external\" href=\"https://www.pypy.org/\">PyPy</a> adalah penerapan Python di Python itu sendiri (penerapan Python 'standar' ada di C). PyPy dapat menawarkan peningkatan kinerja yang substansial, biasanya untuk aplikasi kelas berat.</p>\n<p>A key aim of the PyPy project is <a class=\"reference external\" href=\"https://www.pypy.org/compat.html\">compatibility</a> with existing Python APIs and libraries.\nDjango is compatible with versions of PyPy corresponding to the supported\nPython versions, but you will need to check the compatibility of other\nlibraries you rely on.</p>\n</section>\n<section id=\"c-implementations-of-python-libraries\">\n<h4>C menerapkan pustaka Python<a class=\"heading-anchor\" href=\"#c-implementations-of-python-libraries\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Beberapa pustaka Python juga menerapkan di C, dan dapat lebih cepat. Mereka bermaksud menawarkan API sama. Catat bahwa masalah kesesuaian dan perbedaan kebiasaan tidak diketahui (dan tidak selalu segera jelas).</p>\n</section>\n</section>\n</section>","rootId":"performance-and-optimization","toc":[{"title":"Kata Pengantar","anchor":"introduction","children":[]},{"title":"Pendekatan umum","anchor":"general-approaches","children":[{"title":"What are you optimizing for?","anchor":"what-are-you-optimizing-for","children":[]},{"title":"Penampilan pembandingan","anchor":"performance-benchmarking","children":[{"title":"Alat-alat Django","anchor":"django-tools","children":[]},{"title":"Layanan pihak-ketiga","anchor":"third-party-services","children":[]}]},{"title":"Mendapatkan hal yang benar dari awal","anchor":"get-things-right-from-the-start","children":[{"title":"Bekerja pada tingkatan sesuai","anchor":"work-at-the-appropriate-level","children":[]}]}]},{"title":"Menembolok","anchor":"caching","children":[{"title":"The caching framework 1","anchor":"the-caching-framework","children":[]},{"title":"cached_property","anchor":"cached-property","children":[]}]},{"title":"Memahami laziness","anchor":"understanding-laziness","children":[{"title":"Laziness di Django","anchor":"laziness-in-django","children":[]}]},{"title":"Basisdata","anchor":"databases","children":[{"title":"Optimasi basisdata","anchor":"database-optimization","children":[]},{"title":"Other database-related tips","anchor":"other-database-related-tips","children":[]}]},{"title":"Penampilan HTTP","anchor":"http-performance","children":[{"title":"Middleware","anchor":"middleware","children":[{"title":"ConditionalGetMiddleware","anchor":"conditionalgetmiddleware","children":[]},{"title":"GZipMiddleware","anchor":"gzipmiddleware","children":[]}]},{"title":"Sesi","anchor":"sessions","children":[{"title":"Menggunakan sesi tembolok","anchor":"using-cached-sessions","children":[]}]},{"title":"Berkas statis","anchor":"static-files","children":[{"title":"ManifestStaticFilesStorage","anchor":"manifeststaticfilesstorage","children":[]},{"title":"\"Minifikasi\"","anchor":"minification","children":[]}]}]},{"title":"Penampilan cetakan","anchor":"template-performance","children":[{"title":"The cached template loader","anchor":"the-cached-template-loader","children":[]}]},{"title":"Menggunakan versi berbeda dari perangkat lunak tersedia","anchor":"using-different-versions-of-available-software","children":[{"title":"Terbaru adalah sering - tetapi tidak selalu - terbaik","anchor":"newer-is-often-but-not-always-better","children":[]},{"title":"Jalan lain ke bahasa cetakan Django","anchor":"alternatives-to-django-s-template-language","children":[]},{"title":"Jalan lain penerapan perangkat lunak","anchor":"alternative-software-implementations","children":[{"title":"PyPy","anchor":"id1","children":[]},{"title":"C menerapkan pustaka Python","anchor":"c-implementations-of-python-libraries","children":[]}]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Menggunakan Django","url":"/id/6.0/topics/"}],"prev":{"docname":"topics/security","title":"Kemananan di Django","url":"/id/6.0/topics/security/"},"next":{"docname":"topics/serialization","title":"Menserialkan obyek-obyek Django","url":"/id/6.0/topics/serialization/"},"formats":{"html":"/id/6.0/topics/performance/","markdown":"/id/6.0/topics/performance.md","json":"/id/6.0/topics/performance.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/topics/performance.txt","official":"https://docs.djangoproject.com/id/6.0/topics/performance/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}